7 Replies Latest reply on Sep 24, 2012 12:02 PM by yunshi

    @PersistenceContext is null in jboss7

    yunshi

      Hi all,

       

      I am migrating jboss4.2.3 -> jboss7 and I am totally blocked by the probleme that @PersistenceContext  always returns me NULL in the EJB jars.

       

      Here are the projects in my deployment :

       

      deployment

      -----------------------------

        - hibernate-ejb-1.jar

           | - src/main/java ... some entities and DAO

          | - src/main/resources

                    | - META-INF

                          | - hibernate.cfg.xml

                          | - persistence.xml

       

      - seam2App.ear

           | - module1.jar

          | - module2.jar

           | - war1.war

       

       

           hibernate-ejb-1.jar offers some DAO functions for my seam2App.ear and other applications. However, in hibernate-ejb-1.jar, all the @PersistenceContext  EntityManager returns NULL.

       

      Here is the persistence.xml

       

      <persistence 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"

                version="1.0">

                     <persistence-unit name="xxx-unit" transaction-type="JTA">

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

                               <jta-data-source>java:/xxxxxDS</jta-data-source>

                               <properties>

                           <property name="hibernate.ejb.cfgfile" value="/META-INF/hibernate.cfg.xml"/>

                          <property name="jboss.entity.manager.factory.jndi.name" value="java:jboss/EntityManagerFactories/xxx"/>

                          <property name="hibernate.session_factory_name" value="java:jboss/hibernate/SessionFactoryxxx"/>

                          <property name="jboss.as.jpa.providerModule" value="org.hibernate:3" />

               </properties>

                </persistence-unit>

      </persistence>

       

       

      In the cosole, I have

       

      12:15:49,752 INFO  [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-2) JNDI bindings for session bean named xxxDAOImpl in deployment unit deployment "xxxxxxx-SNAPSHOT.jar" are as follows:

       

       

              java:global/xxxxxx-SNAPSHOT/xxxxxxDAOImpl!com.xxxx.dao.common.remotexxxxxxDAORemote

              java:app/xxxxxx-SNAPSHOT/xxxxxxDAOImpl!com.xxxxxx.dao.common.remote.xxxxxxDAORemote

              java:module/xxxxxxDAOImpl!com.xxxxxx.dao.common.remote.xxxxxxDAORemote

              java:jboss/exported/xxxxxx-SNAPSHOT/xxxxxxDAOImpl!com.xxxxxx.dao.common.remote.xxxxxxDAORemote

              java:global/xxxxxx-SNAPSHOT/xxxxxxDAOImpl!com.xxxxxx.dao.common.local.xxxxxxDAOLocal

              java:app/xxxxxx-SNAPSHOT/xxxxxxDAOImpl!com.xxxxxx.dao.common.local.xxxxxxDAOLocal

              java:module/xxxxxxDAOImpl!com.xxxxxx.dao.common.local.xxxxxxDAOLocal

       

       

      In other applications who depend on hibernate-ejb-1.jar, I tried to get the Ejb like this:

       

           context = new InitialContext();

           DAO dao=  (DAO ) context. lookup("java:global/xxxxxx-SNAPSHOT/xxxxxxDAOImpl!com.xxxxxx.dao.common.local.xxxxxxDAOLocal");

       

       

      DAO.java looks like this

       

      @Stateless

      public class DAOImpl implements DAOLocal, DAORemote{

       

          @PersistenceContext

          private EntityManager entityManager;

       

       

                public List<Bean> findAll() {

                          Query query = createNamedQuery(Bean.FIND_ALL);

                          if(query!=null)

                                    return query.getResultList();

                          return null;

                }

      }

       

      However, I have the NPE because @PersistenceContext  EntityManager entityManager  is always null.

       

      Could anyone help me plz with an idea or some experiences ?

       

      Thanks very much in advance.

        • 1. Re: @PersistenceContext is null in jboss7
          wdfink

          From the first look I can't see a problem.

           

          Do you see any warning/error in the logfile during the deploy and first access?

          What if you strip it to a simple example, just one SLSB and one entity?

          • 2. Re: @PersistenceContext is null in jboss7
            nickarls

            Could you give a dump of the actual hibernate-ejb-1.jar (as I hope  you don't have the persistence.xml under src/main/resources/META-INF)?

            • 3. Re: @PersistenceContext is null in jboss7
              yunshi

              Thanks Wolf-Dieter et Nicklas,

               

              Finally find out the probleme. It has nothing to do with Ejb, neither Hibernate, but classloading I think.

               

              My probleme is

               

              I have a parent DAO  which was placed in a module

               

              public abstract class ParentDAO{

               

                 @PersistenceContext

                 private EntityManager em;

               

                 // some basic functions...

               

              }

               

              Then other DAO extends the parent DAO and they are placed in deployment

               

              @Stateless

              public class DAO extends ParentDAO implements LocalDAO, RemoteDAO{

               

                 // other dao functions using em from parentDAO...

               

              }

               

              After I move the ParentDAO project out to deployment from the module, it works.

               

              I cannot really figurer out why but I think its the classloading probleme.

              • 4. Re: @PersistenceContext is null in jboss7
                wdfink

                AFAIK there was an issue with the injection.

                Do you use the latest version (AS7.2 upstream or nightly build)?

                • 5. Re: @PersistenceContext is null in jboss7
                  yunshi

                  Nope, I use AS 7.1.1.Final.

                  • 6. Re: @PersistenceContext is null in jboss7
                    smarlow

                    Does this involve multiple application deployments or a single ear/deployment?

                    • 7. Re: @PersistenceContext is null in jboss7
                      yunshi

                      Il involves multiple application deployment.

                       

                      I have serveral ejb projects which are independent with each other. I had this ParentDAO.java in a seperated project aiming to offer some basic DAO functions for all other ejb projects.

                       

                      projetA.jar

                      --------------------

                      public abstract class ParentDAO{

                       

                         @PersistenceContext

                         private EntityManager em;

                       

                         // some basic functions...

                       

                      }

                       

                      projetB.jar or projetC.jar

                      ----------------------------------

                      @Stateless

                      public class DAO extends ParentDAO implements LocalDAO, RemoteDAO{

                       

                         // other dao functions using em from parentDAO...

                       

                      }

                       

                       

                       

                       

                      The @persistenceContext in the DAO returns NULL.

                       

                       

                       

                      Otherwise, if I copy the ParentDAO.java into the ejb projects, which means that @PersistenceContext is in the same project of the EJB projects projectB and projectC. @PersistenceContext  returns the right EntityManager.

                       

                      I donot understand... does it mean that I have to @PersistenceContext can only be put in the same ejb project where it's called ?