6 Replies Latest reply on Jul 30, 2010 7:09 AM by Ehud Hoggeg

    @PersistenceContext is null

    Ehud Hoggeg Newbie



      I am somewhat new to JBoss, though I struggle with it for sometime now.


      I am using

      • JBoss 6 (, though same issue seems to happen for JBoss 5.1 release.
      • Hibernate 3.5.3.

      I develop using eclipse galileo and JbossTools plugin.

      I have 2 projects - a dynamic-web project for the "web tier" (Web) and an EJB project (App) for the "application tier". These are deployed into

      /default/deploy/Web.war and /default/deploy/App.jar.

      I am using MySql 5.1 (mysql-connector-java-5.1.10-bin.jar reside in the /default/deploy/lib to which I deploy my projects)


      My problem is that my @PersistenceContext EntityManger, which reside in a @Stateless SB is null.


      @Stateless(name = "EnvironmentManager")
      public class EnvironmentManager implements EnvironmentManagerRemote, EnvironmentManagerLocal {


          protected EntityManager em;



      I have tried initializing it in an intialization block, like so:

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


      This worked (but this not a valid workaround for my purpose)


      I looked around and did many attempts to solve this and find the problem. I might be missing something foundamental.


      My persistence.xml file is attached. It is deplyed to the /META-INF in the EJB project (App.jar) and looks fine.

      A data-source (*-ds.xml) is defined on the server /deploy folder and connection succeed (I managed to "manually" access it and invoke queries).

        • 1. Re: @PersistenceContext is null
          Zemian Deng Apprentice

          The persistence's trancaction-type should be either "RESOURCE_LOCAL" or "JTA".


          To be able to inject container's EM in your EJB as you described:

              protected EntityManager  em;


          You would need to configure JTA in your persistence.xml.


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


          If you still want to use  RESOURCE_LOCAL type, I think you can still inject it, using EM Factory in your EJB, like this:

              private EntityManagerFactory factory;


          But then your container will not able to manage your EM (persistence context)! Use JTA is more proper way in a EJB container in normal cases.

          1 of 1 people found this helpful
          • 2. Re: @PersistenceContext is null
            Ehud Hoggeg Newbie

            Thank you for your quick reply Zemian,


            Unfortunatly that didnt do the trick ... I applied the change as you described, but the EntityManager is still null.


            Can you think of something else? Id appriciate anything. Thank you!


            My console log is attached.


            • 3. Re: @PersistenceContext is null
              Zemian Deng Apprentice

              persistence.xml defined in WAR is only accessable to EJBs defined within that scope. Are you having trouble with EJBs in WAR or in your separated EJB jar?


              If deploying EJBs jar and WAR together that need to use persistence unit, try to package persistence unit jar in a EAR's lib properly, that way, both WAR and EJBs will have access to it.


              Try to read upon proper EJB/WAR/EAR packaging and deployment.

              1 of 1 people found this helpful
              • 4. Re: @PersistenceContext is null
                Ehud Hoggeg Newbie

                Thank you and sorry for the delay, I read around about deployment and packaging.

                I switched to EAR deployment, it deploy fine (no exceptions and my code is running), but the PersistenceContext is still null...


                My /deploy dir looks like this:



                |     App.jar

                |     |     com

                |     |     | ... (my packages hierarchy)

                |     |     META-INF

                |     |     |-----persistence.xml

                |     Http.war

                |     |     ... (my servlets, jsp`s and other js/css/...)

                |     META-INF

                |     |----application.xml



                My EJB (a @Stateless SB, which implement @Local and @Remote interfaces) is located in the App.jar.

                Only access to the entityManager is done by that SB (for now), i.e. from the jar. This is where I get the null pointer.

                • 5. Re: @PersistenceContext is null
                  jaikiran pai Master

                  How do you get hold of the EJB in your code? Are you sure you are doing a JNDI lookup? Can you post your client code where this lookup is happening? And also post the code where you are checking the injected persistence context.

                  • 6. Re: @PersistenceContext is null
                    Ehud Hoggeg Newbie

                    Thank you Jaikiran! You got it. It was the JNDI call.


                    Fix was to call something like


                         (StatelessSB) getInitialContext()*.lookup("<EAR name>/statelessSB/local");


                    where statelessSB is set on name attribute of the Stateless annotation, on the SLSB class StatelessSB .

                    * getInitialContext() return a new javax.naming.InitialContext()


                    Zemian, many thanks as well of course.