7 Replies Latest reply on Mar 14, 2007 2:56 PM by Andrew Rubinger

    PersistenceContext and EntityManager

    R M Newbie

      Hi everybody,

      I've got a problem with this "magical" @PersistenceContext... It doesn't inject any EntityManager :( ... my entityManager variable always evaluates to null and generates an horrible NullPointerException...

      I already googled for hours to find a solution and the only thing I found was about using an EntityManagerFactory which also had to be injected and which stayed to the "null" value as well...

      Does anybody knows something about that ?

      Thanks in advance !


        • 1. Re: PersistenceContext and EntityManager
          Dogort Tucky Newbie


          hard to say what your mistake is. for me it works. see the trailblazer from jboss which is very helpful and up-to-date:

          the basic idea if you are using your own datasource is about that

          1) code incetion:

          @PersistenceContext(unitName = "YourEntityManagerName")

          2) a persistence.xml in metainf about like this:
           <persistence-unit name="YourEntityManagerName">
           <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
           <property name="hibernate.cache.provider_class" value="org.jboss.ejb3.entity.TreeCacheProviderHook"/>
           <property name="hibernate.treecache.mbean.object_name" value="jboss.cache:service=EJB3EntityTreeCache"/>
           <property name="hibernate.hbm2ddl.auto" value="update"/>

          3) define your datasource in the deploy folder of the server
          <?xml version="1.0" encoding="UTF-8"?>
          <!-- this file goes to server/xxx/deploy -->

          but as mentioned... check out the tutorial, they did a very well job


          patrick heusser

          • 2. Re: PersistenceContext and EntityManager
            R M Newbie

            Thanks for your answer...

            I certainly missed something... The tutorial is really well done, but all they tell about using the entity manager is only that it should be preceded by the @PersistenceContext annotation.

            This is what my code looks like...

            public class Mediastore implements Serializable {
             private EntityManager entityManager ;
             private String hello = "Hello World";
             private static final long serialVersionUID = -2416336874715455139L;
             public Mediastore(){
             if(entityManager != null){
             System.out.println("Entity manager created successfully");
             else {
             System.out.println("Entity Manager is null !");

            This is the code which helped me to say that the EntityManaged was not injected correctly, after having had a lot of null pointer exceptions...

            Here's my persistence.xml file :
             <persistence-unit name="mediastore">
             <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
             <property name="hibernate.cache.use_query_cache" value="true"/>
             <property name="hibernate.show_sql" value="true"/>

            My "mediastore-ds.xml" file :

            <?xml version="1.0" encoding="UTF-8"?>

            My ear file is packaged like this :


            I hope this could help someone to see what I did wrong.

            Thanks in advance,


            • 3. Re: PersistenceContext and EntityManager
              R M Newbie

              In fact, I think I'm really doing something wrong ... the @EJB annotation doesn't work either ...

              • 4. Re: PersistenceContext and EntityManager
                Dan Hulsmann Newbie

                Hi rmic

                I have come across exactly the same problem. Not sure whether it's us, JBoss or Hibernate.

                Have you been able to shed any light on this yet?


                • 5. Re: PersistenceContext and EntityManager
                  Lewis Gass Newbie

                  If you are relying on the debug messages you are printing out in the constructor that is your problem, since the values are injected AFTER the object is constructed.

                  • 6. Re: PersistenceContext and EntityManager
                    Hugo Newbie

                    First: Your answer did the trick,Thanks a lot!
                    secondly: WHY?
                    Why is injection done AFTER the constructor? I made those nice final fields to be injected and now they fail ;-(

                    • 7. Re: PersistenceContext and EntityManager
                      Andrew Rubinger Master


                      Why is injection done AFTER the constructor?

                      How is it possible to inject values into an object *before* the object itself has been created?