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

    PersistenceContext and EntityManager

    rmic

      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 !

      rmic.

        • 1. Re: PersistenceContext and EntityManager
          wauwau0977

          hi

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

          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>
           <persistence-unit name="YourEntityManagerName">
           <jta-data-source>java:/OpenPaintingDSMySql</jta-data-source>
           <properties>
           <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"/>
           </properties>
           </persistence-unit>
          </persistence>


          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 -->
          <datasources>
           <local-tx-datasource>
          
           <jndi-name>OpenPaintingDSMySql</jndi-name>
           <connection-url>jdbc:mysql://localhost:3306/openpainting</connection-url>
           <driver-class>com.mysql.jdbc.Driver</driver-class>
           <user-name>xxxx</user-name>
           <password>yyyy</password>
          
           <min-pool-size>2</min-pool-size>
           <max-pool-size>20</max-pool-size>
           <idle-timeout-minutes>5</idle-timeout-minutes>
          
           <exception-sorter-class-name>com.mysql.jdbc.integration.jboss.ExtendedMysqlExceptionSorter</exception-sorter-class-name>
           <valid-connection-checker-class-name>
           com.mysql.jdbc.integration.jboss.MysqlValidConnectionChecker</valid-connection-checker-class-name>
          
           </local-tx-datasource>
          </datasources>


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

          regards,
          patrick

          --
          patrick heusser
          http://www.x8ing.com/fractal/

          • 2. Re: PersistenceContext and EntityManager
            rmic

            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 {
             @PersistenceContext(unitName="mediastore")
             private EntityManager entityManager ;
             private String hello = "Hello World";
            
             private static final long serialVersionUID = -2416336874715455139L;
            
             @SuppressWarnings("unchecked")
             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>
             <persistence-unit name="mediastore">
             <jta-data-source>java:/mediastoreDS</jta-data-source>
             <properties>
             <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
             <property name="hibernate.cache.use_query_cache" value="true"/>
             <property name="hibernate.show_sql" value="true"/>
             </properties>
             </persistence-unit>
            </persistence>
            


            My "mediastore-ds.xml" file :

            <?xml version="1.0" encoding="UTF-8"?>
            
            <datasources>
             <local-tx-datasource>
             <jndi-name>mediastoreDS</jndi-name>
             <connection-url>jdbc:postgresql://localhost/mediastore</connection-url>
             <driver-class>org.postgresql.Driver</driver-class>
             <user-name>postgres</user-name>
             <password>postgres</password>
             <min-pool-size>5</min-pool-size>
             <max-pool-size>20</max-pool-size>
             <idle-timeout-minutes>10</idle-timeout-minutes>
             <track-statements>true</track-statements>
             </local-tx-datasource>
            </datasources>
            


            My ear file is packaged like this :

            
            mediastore.ear
             /META-INF
             application.xml
             mediastore.jar
             /META-INF
             persistence.xml
             /packages/classfiles
            
             mediastore.war
             /WEB-INF
             web.xml
             faces-config.xml
             /lib
             jarfiles
             /classes
             packages/classes
             index.jsp
             home.jsp
            
            
            


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

            Thanks in advance,

            rmic.

            • 3. Re: PersistenceContext and EntityManager
              rmic

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

              • 4. Re: PersistenceContext and EntityManager
                bluevervet

                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?

                Thanks
                dh

                • 5. Re: PersistenceContext and EntityManager
                  elponderador

                  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
                    holeinone

                    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
                      alrubinger

                       

                      Why is injection done AFTER the constructor?


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

                      S,
                      ALR