9

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;
}
jeff
  • 3,618
  • 9
  • 48
  • 101
  • 2
    show us how you obtain an instance of `CatalogFacade`. And what is it - a pojo, an ejb, or? – Bozho Dec 07 '10 at 21:23

2 Answers2

13

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.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • I'm using Eclipse and tomcat. CatalogFacade is a listener. – jeff Dec 07 '10 at 22:03
  • 1
    @jeff - then you can't use `@PersistenceUnit`. You need some context that will inject the dependency. – Bozho Dec 07 '10 at 22:05
  • That is not my understanding: I should be a able to use @PersistenceUnit for application managed context – YoYo Jan 18 '13 at 08:01
  • yes, but you need a managed context. as far as I understood, his listener is not instantiated that way – Bozho Jan 18 '13 at 09:25
0

For some reasons this happens when using this in you persistence.xml

<provider>org.hibernate.ejb.HibernatePersistence</provider>

switching to

<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

fixes the problem

ACV
  • 9,964
  • 5
  • 76
  • 81