2 Replies Latest reply on Jan 3, 2009 4:51 AM by tristans

    Can't inject PersistenceContext, NullPointerException when u

    tristans

      I have been working through a JBoss (5.0.0.CR2) example using the tutorial at http://www.netbeans.org/kb/55/ejb30-jboss.html#Exercise_5 (however using Netbeans 6.5).

      I have an entity set up and upon deployment it is creating the table on the database. My problem is when I try to use a SLSB as a facade to retrieve entities back from the database. The generated code is as follows:


      @Stateless
      public class CustomerFacade implements CustomerFacadeLocal {
       @PersistenceContext
       private EntityManager em;
      
       public void create(Customer customer) {
       em.persist(customer);
       }
      
       public void edit(Customer customer) {
       em.merge(customer);
       }
      
       public void remove(Customer customer) {
       em.remove(em.merge(customer));
       }
      
       public Customer find(Object id) {
       return em.find(Customer.class, id);
       }
      
       public List<Customer> findAll() {
       return em.createQuery("select object(o) from Customer as o").getResultList();
       }
      
      }


      Unfortunately when I run this code, I get a NullPointerException upon usage of em because the PersistenceContext isn't being injected (I presume). This I'm sure is a config error, however I have also tried
      @PersistenceUnit(unitName = "PrototypeCRMSystem-ejbPU")
       private EntityManagerFactory emf;
      
       private EntityManager em = emf.createEntityManager();
      which gives the same problem when accessing emf.

      My persitence.xml is as follows:

      <?xml version="1.0" encoding="UTF-8"?>
      <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
       <persistence-unit name="PrototypeCRMSystem-ejbPU" transaction-type="JTA">
       <provider>org.hibernate.ejb.HibernatePersistence</provider>
       <jta-data-source>java:/OracleDS</jta-data-source>
       <properties>
       <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9iDialect"/>
       <property name="hibernate.hbm2ddl.auto" value="update"/>
       <property name="hibernate.show_sql" value="true" />
       </properties>
       </persistence-unit>
      </persistence>
      


      Am I missing something configuration-wise or is there a problem?

      Thanks