Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
170 views
in Technique[技术] by (71.8m points)

java - @PersistenceUnit annotation won't create an EntityManageFactory emf=null

I'm try to use the Sun Java PetStore Demo.
In the CatalogFacade class there is the following annotation:

@PersistenceUnit(unitName="myPetStorePU")
private EntityManagerFactory emf;

In all the methods of the CatalogFacade Sun has:

    EntityManager em = emf.createEntityManager();

But I am getting a null pointer exception for emf when trying to createEntityManager. But... if I add the following line above that line as such

    EntityManagerFactory emf = javax.persistence.Persistence.createEntityManagerFactory("myPetStorePU");
    EntityManager em = emf.createEntityManager();

then emf gets successfully created and the persistence unit myPetStorePU also successfully connects to the database. So it looks like persistence.xml syntax and its location is correct. I'd like to understand why the annotation doesn't work since I think there was a reason for just using the annotation as opposed to adding the createEntityManagerFactory line in every method.

My src/META-INF/persistence.xml file looks like this:

<persistence-unit name="myPetStorePU">
    <description>Petstore Persistence Unit</description>
    <provider>oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider</provider>

  <class>com.sun.javaee.blueprints.petstore.model.Tag</class>
  <class>com.sun.javaee.blueprints.petstore.model.SellerContactInfo</class>
  <class>com.sun.javaee.blueprints.petstore.model.Product</class>
  <class>com.sun.javaee.blueprints.petstore.model.Item</class>
  <class>com.sun.javaee.blueprints.petstore.model.Category</class>
  <class>com.sun.javaee.blueprints.petstore.model.Address</class>
  <class>com.sun.javaee.blueprints.petstore.model.ZipLocation</class>
    <properties>
        <property name="toplink.jdbc.driver" value="oracle.jdbc.driver.OracleDriver"/>
        <property name="toplink.jdbc.url" value="jdbc:oracle:thin:@#############"/>
        <property name="toplink.jdbc.user" value="####"/>
        <property name="toplink.jdbc.password" value="#####"/>
        <property name="toplink.logging.level" value="INFO"/>
    </properties>

</persistence-unit>

Edit: CatalogFacade is in the petstore.model package and implements the ServletContextListener

<listener>
    <listener-class>com.sun.javaee.blueprints.petstore.model.CatalogFacade</listener-class>
</listener>

in the index.jsp Sun has the following:

<%
CatalogFacade cf = (CatalogFacade)config.getServletContext().getAttribute("CatalogFacade");
List<Tag> tags=cf.getTagsInChunk(0, 12);
%>


public List<Tag> getTagsInChunk(int start, int chunkSize) {
//The next line is required since the @PersistenceUnit annotation at the top of this class does not work
    EntityManagerFactory emf = javax.persistence.Persistence.createEntityManagerFactory("myPetStorePU");
    EntityManager em = emf.createEntityManager();
    System.out.println("Entity manager " + emf);
    Query query = em.createQuery("SELECT t FROM Tag t ORDER BY t.refCount DESC, t.tag");
    List<Tag> tags = query.setFirstResult(start).setMaxResults(chunkSize).getResultList();
    em.close();
    return tags;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If the annotated object is not managed by a container (either spring/CDI/EJB container), nothing gets injected into it.

So depending on your environment, obtain a contextual instance of that object.

If you are not using any of the above technologies (spring/CDI/EJB) - then you can't use @PersistenceUnit and @PersistenceContext. Use the manual way to obtain the unit.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...