Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@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;
}
like image 597
jeff Avatar asked Dec 07 '10 21:12

jeff


People also ask

What is the significance of @PersistenceUnit annotation?

Annotation Type PersistenceUnit. Expresses a dependency on an EntityManagerFactory and its associated persistence unit. (Optional) The name by which the entity manager factory is to be accessed in the environment referencing context; not needed when dependency injection is used.

How do you make an EntityManagerFactory?

To create the EntityManagerFactory you need to create to persistence. xml file first. The file is where you configure the JPA. This file must be placed inside the META-INF directory in your program working directory.

What is difference between EntityManagerFactory and EntityManager?

EntityManagerFactory vs EntityManagerWhile EntityManagerFactory instances are thread-safe, EntityManager instances are not. The injected JPA EntityManager behave just like an EntityManager fetched from an application server's JNDI environment, as defined by the JPA specification.

Where does persistence XML go in spring boot?

Spring Boot will not search for or use a META-INF/persistence. xml by default. If you prefer to use a traditional persistence. xml , you need to define your own @Bean of type LocalEntityManagerFactoryBean (with an ID of 'entityManagerFactory') and set the persistence unit name there.


1 Answers

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.

like image 123
Bozho Avatar answered Oct 17 '22 09:10

Bozho