1 2 Previous Next 19 Replies Latest reply on May 3, 2013 4:52 PM by Nicklas Karlsson

    AS 7 Not inject EntityManager

    Vitor Eduardo da Silva Newbie

      Hi all.

       

      I know that it is not the first time that someone posts a commentary about this topic. However, see the question bellow and then, think you could help me.

       

      I have an abstract facade like this:

       

      public abstract class AbstractFacade<T, I> {  
            
          private Class<T> entityClass;  
            
          public AbstractFacade(Class<T> entityClass) {  
                
              this.entityClass = entityClass;  
                
          }  
            
          public abstract EntityManager getEntityManager();  
            
            
          public void create(T entity) {  
                
              getEntityManager().persist(entity);  
                
          }  
            
          public void edit(T entity) {  
                
              getEntityManager().merge(entity);  
                
          }  
            
          public void remove(T entity) {  
                
              getEntityManager().remove(getEntityManager().merge(entity));  
                
          }  
            
          public T find(I id) {  
                
              return getEntityManager().find(entityClass, id);  
                
          }  
            
      }  
      
      
      

       

      and an implementation like bellow:

       

       

      @Stateless  
      public class UserBeanFacade extends AbstractFacade<Users, UsersId> {  
            
          @PersistenceContext(unitName = "ELBapps")  
          private EntityManager manager;  
            
          public UserBeanFacade() {  
              super(Users.class);  
          }  
        
          @Override  
          public EntityManager getEntityManager() {  
        
              return manager;  
                
          }  
        
      }  
      
      
      

       

      when the line

       

       

      return getEntityManager().find(entityClass, id); 
      

       

      from abstract facade is executed, generates a NullPointerException:

       

      Caused by: java.lang.NullPointerException  
          at br.com.leaobaio.elbapps.dataaccess.AbstractFacade.find(AbstractFacade.java:38) [elbappsback.jar:]
      
      

       

      I have an EJB and a Web modules. My beans.xml is located in META-INF into ejbModule.

       

      I am using: Eclipse Juno; AS 7, JEE 6; EJB 3.1; JPA; and JTA data source.

       

      My data source:

       

      <datasource jta="true" jndi-name="java:/ELBapps" pool-name="ELBapps" enabled="true" use-java-context="true">  
                          <connection-url>jdbc:postgresql:elbapps</connection-url>  
                          <driver>org.postgresql</driver>  
                          <transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation>  
                          <pool>  
                              <min-pool-size>10</min-pool-size>  
                              <max-pool-size>100</max-pool-size>  
                              <prefill>true</prefill>  
                          </pool>  
                          <security>  
                              <user-name>postgres</user-name>  
                              <password>postgres</password>  
                          </security>  
                          <statement>  
                              <prepared-statement-cache-size>32</prepared-statement-cache-size>  
                              <share-prepared-statements>true</share-prepared-statements>  
                          </statement>  
                      </datasource>
      

       

      My persistence.xml

       

       

      <?xml version="1.0" encoding="UTF-8"?>  
      <persistence version="2.0"  
          xmlns="<a target="_blank" href="http://java.sun.com/xml/ns/persistence"">http://java.sun.com/xml/ns/persistence"</a>  
          xmlns<img src="http://javafree.uol.com.br/forum/images/smiles/icon_mad.gif">si="<a target="_blank" href="http://www.w3.org/2001/XMLSchema-instance"">http://www.w3.org/2001/XMLSchema-instance"</a>  
          xsi:schemaLocation="<a target="_blank" href="http://java.sun.com/xml/ns/persistence">http://java.sun.com/xml/ns/persistence</a> <a target="_blank" href="http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">">http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"></a>  
            
          <persistence-unit name="ELBapps" transaction-type="JTA">  
              <provider>org.hibernate.ejb.HibernatePersistence</provider>  
              <jta-data-source>java:/ELBapps</jta-data-source>  
                
              <class>br.com.leaobaio.elbapps.dataaccess.entities.Profile</class>  
              <class>br.com.leaobaio.elbapps.dataaccess.entities.Resource</class>  
              <class>br.com.leaobaio.elbapps.dataaccess.entities.Article</class>  
              <class>br.com.leaobaio.elbapps.dataaccess.entities.ArticlesStatus</class>  
              <class>br.com.leaobaio.elbapps.dataaccess.entities.Issue</class>  
              <class>br.com.leaobaio.elbapps.dataaccess.entities.Publication</class>  
              <class>br.com.leaobaio.elbapps.dataaccess.entities.Users</class>  
                
              <properties>  
                  <property name="hibernate.hbm2ddl.auto" value="update"/>  
                  <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>  
                  <property name="hibernate.archive.autodetection" value="class"/>  
                  <property name="hibernate.show_sql" value="true"/>  
                  <property name="hibernate.format_sql" value="true"/>  
              </properties>  
          </persistence-unit>  
            
      </persistence>
      
      

       

      Could help me?

       

      Vitor Eduardo

        • 1. Re: AS 7 Not inject EntityManager
          Scott Marlow Master

          What happens if you try the same with the latest WildFly (previously called AS8) nightly build?  You can access the download from here.

           

          Enabling TRACE logging for org.jboss.as.jpa might also be helpful.  Instructions for enabling TRACE logging are here.

          • 2. Re: AS 7 Not inject EntityManager
            Nicklas Karlsson Master

            Where is the persistence.xml located? Is there anything in the log that indicates it's being picked up on deployment?

            • 3. Re: AS 7 Not inject EntityManager
              Vitor Eduardo da Silva Newbie

              Hi sir, thank you for your dedicated time.

               

              Well, my persistence.xml is located in META-INF into ejbModule folder which is deployed in the WEB-INF/lib inside the backend (EJB module) jar.

               

              During the server startup these lines were displayed in Eclipse Console view:

               

              08:35:24,464 INFO  [org.hibernate.tool.hbm2ddl.SchemaUpdate] (MSC service thread 1-2) HHH000228: Running hbm2ddl schema update
              08:35:24,465 INFO  [org.hibernate.tool.hbm2ddl.SchemaUpdate] (MSC service thread 1-2) HHH000102: Fetching database metadata
              08:35:24,480 INFO  [org.hibernate.tool.hbm2ddl.SchemaUpdate] (MSC service thread 1-2) HHH000396: Updating schema
              08:35:24,928 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-2) HHH000261: Table found: public.article
              08:35:24,930 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-2) HHH000037: Columns: [id, author, title, text, keywords, status, theme, issue_id, subtitle, synopsis]
              08:35:24,933 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-2) HHH000108: Foreign keys: [fkd458ccf6fa8245d5]
              08:35:24,935 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-2) HHH000126: Indexes: [article_pkey]
              08:35:24,984 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-2) HHH000261: Table found: public.articlesstatus
              08:35:24,985 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-2) HHH000037: Columns: [id, username, article_id, changedate, cstatus, pstatus, cpathtext]
              08:35:24,987 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-2) HHH000108: Foreign keys: [fke905406f6dfc31f5, fke905406fc1af4599]
              08:35:24,988 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-2) HHH000126: Indexes: [articlesstatus_pkey]
              08:35:25,030 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-2) HHH000261: Table found: public.issue
              08:35:25,031 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-2) HHH000037: Columns: [id, edition, username, publication_id, path, entrydate, type, note]
              08:35:25,032 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-2) HHH000108: Foreign keys: [fk5fda8d9c1af4599, fk5fda8d9d64c28b5]
              08:35:25,034 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-2) HHH000126: Indexes: [issue_pkey]
              08:35:25,069 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-2) HHH000261: Table found: public.profile
              08:35:25,071 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-2) HHH000037: Columns: [id, status]
              08:35:25,072 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-2) HHH000108: Foreign keys: []
              08:35:25,073 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-2) HHH000126: Indexes: [profile_pkey]
              08:35:25,117 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-2) HHH000261: Table found: public.publication
              08:35:25,119 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-2) HHH000037: Columns: [id, username, status, standardnumber, closingday, name, entryday, periodicity, type, effectivedate]
              08:35:25,121 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-2) HHH000108: Foreign keys: [fkbfbba22cc1af4599]
              08:35:25,122 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-2) HHH000126: Indexes: [publication_pkey]
              08:35:25,157 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-2) HHH000261: Table found: public.resource
              08:35:25,158 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-2) HHH000037: Columns: [id, module, friendlyname, status, name]
              08:35:25,159 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-2) HHH000108: Foreign keys: []
              08:35:25,160 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-2) HHH000126: Indexes: [resource_pkey]
              08:35:25,203 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-2) HHH000261: Table found: public.resourcesprofile
              08:35:25,204 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-2) HHH000037: Columns: [resource_id, profile_id]
              08:35:25,206 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-2) HHH000108: Foreign keys: [fk291b87e49f40625f, fk291b87e43ba41115]
              08:35:25,207 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-2) HHH000126: Indexes: []
              08:35:25,255 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-2) HHH000261: Table found: public.userprofiles
              08:35:25,256 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-2) HHH000037: Columns: [username, profile_id]
              08:35:25,257 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-2) HHH000108: Foreign keys: [fk92a29cb53ba41115, fk92a29cb5c1af4599]
              08:35:25,258 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-2) HHH000126: Indexes: []
              08:35:25,296 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-2) HHH000261: Table found: public.users
              08:35:25,309 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-2) HHH000037: Columns: [username, email, name, password]
              08:35:25,310 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-2) HHH000108: Foreign keys: []
              08:35:25,311 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-2) HHH000126: Indexes: [users_pkey]
              08:35:25,312 INFO  [org.hibernate.tool.hbm2ddl.SchemaUpdate] (MSC service thread 1-2) HHH000232: Schema update complete
              

               

              To me, this shows that the persistence was picked up.

              • 4. Re: AS 7 Not inject EntityManager
                Nicklas Karlsson Master

                It would appear so, although I have a bit of a problem parsing "Well, my persistence.xml is located in META-INF into ejbModule folder which is deployed in the WEB-INF/lib inside the backend (EJB module) jar."

                 

                Can you show a graph of the the deployment?

                 

                What happens if you try bootstrapping JPA with

                 

                EntityManagerFactory emf = Persistence.createEntityManagerFactory("ELBapps"); and move from there?

                • 5. Re: AS 7 Not inject EntityManager
                  Vitor Eduardo da Silva Newbie

                  Maybe the persistence.xml was not picked up. See the log:

                   

                   

                  Caused by: java.lang.NullPointerException
                            at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus(JtaStatusHelper.java:73) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
                            at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.isActive(JtaStatusHelper.java:115) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
                            at org.hibernate.engine.transaction.internal.jta.CMTTransaction.join(CMTTransaction.java:149) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
                            at org.hibernate.ejb.AbstractEntityManagerImpl.joinTransaction(AbstractEntityManagerImpl.java:1207) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
                            at org.hibernate.ejb.AbstractEntityManagerImpl.postInit(AbstractEntityManagerImpl.java:176) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
                            at org.hibernate.ejb.EntityManagerImpl.<init>(EntityManagerImpl.java:89) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
                            at org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:125) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
                            at org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:120) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
                            at br.com.leaobaio.elbapps.business.UserBeanFacade.getEntityManager(UserBeanFacade.java:27) [elbappsback.jar:]
                            at br.com.leaobaio.elbapps.dataaccess.AbstractFacade.find(AbstractFacade.java:38) [elbappsback.jar:]
                            at br.com.leaobaio.elbapps.views.LoginView$1.buttonClick(LoginView.java:156) [classes:]
                            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_17]
                            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_17]
                            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_17]
                            at java.lang.reflect.Method.invoke(Method.java:601) [rt.jar:1.7.0_17]
                            at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:508) [vaadin-server-7.0.4.jar:7.0.4]
                  
                  

                   

                  This occours when the line bellow is executed:

                   

                   

                  manager = emf.createEntityManager();
                  

                   

                  The method is written like bellow:

                   

                  @Override
                            public EntityManager getEntityManager() {
                    
                                      EntityManagerFactory emf = Persistence.createEntityManagerFactory("ELBapps");
                    
                                      manager = emf.createEntityManager();
                  
                  
                                      return manager;
                    
                            }
                  
                  
                  • 6. Re: AS 7 Not inject EntityManager
                    Scott Marlow Master

                    Sounds like you succeeded in getting an entitymanagerfactory, your just missing quite a bit of configuration properties to bootstrap this way.  Most applications don't use Persistence.createEntityManagerFactory() directly as if you were writing a standalone Java application.

                    • 7. Re: AS 7 Not inject EntityManager
                      Scott Marlow Master

                      In other words, I think calling Persistence.createEntityManagerFactory() is an interesting test but probably not the right path for you (unless your really looking to avoid EE JPA container level services .

                      • 8. Re: AS 7 Not inject EntityManager
                        Vitor Eduardo da Silva Newbie

                        Scott Marlow wrote:

                         

                        Most applications don't use Persistence.createEntityManagerFactory() directly as if you were writing a standalone Java application.

                         

                        It is just a little test to suggested by Nicklas Karlsson. Have you an idea what could heppen?

                        • 9. Re: AS 7 Not inject EntityManager
                          Scott Marlow Master

                          You are getting the null pointer exception because you didn't pass enough properties into the Persistence.createEntityManagerFactory().  For example, you would need to tell Hibernate how to locate the active JTA transaction from the current thread. 

                           

                          When you are ready to go back to what you were trying previously, you could follow my first response (if you want.)  If you really want to continue using Persistence.createEntityManagerFactory(), that is harder but you could find an example on this forum.

                          • 10. Re: AS 7 Not inject EntityManager
                            Vitor Eduardo da Silva Newbie

                            It is not the path, I am trying to use CDI. Then this will be managed by the server.

                            • 11. Re: AS 7 Not inject EntityManager
                              Vitor Eduardo da Silva Newbie

                              Maintaining the CDI idea, are there needed changes to do in persistence.xml?

                               

                              Scott, my intention is to host this app at OpenShift. Then my idea is just to start, to leave everything working fine. After, host there, and continue the development.

                               

                              Suggestions?

                              • 12. Re: AS 7 Not inject EntityManager
                                Scott Marlow Master

                                Sure, revert your recent change to use Persistence.createEntityManagerFactory() and enable TRACE logging to see what is happening and why.

                                 

                                Enabling TRACE logging for org.jboss.as.jpa might also be helpful.  Instructions for enabling TRACEhttps://docs.jboss.org/author/display/AS72/JPA+Reference+Guide#JPAReferenceGuide-Troubleshootinglogging are here.

                                • 13. Re: AS 7 Not inject EntityManager
                                  Nicklas Karlsson Master

                                  I mostly suggested it to see if the persistence.xml would be found at the expected location. Not that I'm sure what the exact message would be if it wasn't.

                                  • 14. Re: AS 7 Not inject EntityManager
                                    Vitor Eduardo da Silva Newbie

                                    Just posting a log with TRACE. This still with CDI.

                                     

                                    13:48:36,055 TRACE [org.jboss.as.jpa] (MSC service thread 1-1) parse persistence.xml: attribute value(0) = ELBapps
                                    13:48:36,056 TRACE [org.jboss.as.jpa] (MSC service thread 1-1) parse persistence.xml: attribute value(1) = JTA
                                    13:48:36,058 TRACE [org.jboss.as.jpa] (MSC service thread 1-1) parse persistence.xml: element=provider
                                    13:48:36,060 TRACE [org.jboss.as.jpa] (MSC service thread 1-1) parse persistence.xml: element=jta-data-source
                                    13:48:36,062 TRACE [org.jboss.as.jpa] (MSC service thread 1-1) parse persistence.xml: element=class
                                    13:48:36,063 TRACE [org.jboss.as.jpa] (MSC service thread 1-1) parse persistence.xml: element=class
                                    13:48:36,064 TRACE [org.jboss.as.jpa] (MSC service thread 1-1) parse persistence.xml: element=class
                                    13:48:36,065 TRACE [org.jboss.as.jpa] (MSC service thread 1-1) parse persistence.xml: element=class
                                    13:48:36,066 TRACE [org.jboss.as.jpa] (MSC service thread 1-1) parse persistence.xml: element=class
                                    13:48:36,067 TRACE [org.jboss.as.jpa] (MSC service thread 1-1) parse persistence.xml: element=class
                                    13:48:36,068 TRACE [org.jboss.as.jpa] (MSC service thread 1-1) parse persistence.xml: element=class
                                    13:48:36,069 TRACE [org.jboss.as.jpa] (MSC service thread 1-1) parse persistence.xml: element=properties
                                    13:48:36,071 TRACE [org.jboss.as.jpa] (MSC service thread 1-1) parse persistence.xml: reached ending persistence-unit tag
                                    13:48:36,072 INFO  [org.jboss.as.jpa] (MSC service thread 1-1) JBAS011401: Read persistence.xml for ELBapps
                                    13:48:36,073 TRACE [org.jboss.as.jpa] (MSC service thread 1-1) PersistenceUnitMetadataImpl(version=2.0) [
                                              name: ELBapps
                                              jtaDataSource: java:/ELBapps
                                              nonJtaDataSource: null
                                              transactionType: JTA
                                              provider: org.hibernate.ejb.HibernatePersistence
                                              classes[
                                                        br.com.leaobaio.elbapps.dataaccess.entities.Profile                    br.com.leaobaio.elbapps.dataaccess.entities.Resource                    br.com.leaobaio.elbapps.dataaccess.entities.Article                    br.com.leaobaio.elbapps.dataaccess.entities.ArticlesStatus                    br.com.leaobaio.elbapps.dataaccess.entities.Issue                    br.com.leaobaio.elbapps.dataaccess.entities.Publication                    br.com.leaobaio.elbapps.dataaccess.entities.Users          ]
                                              packages[
                                              ]
                                              mappingFiles[
                                              ]
                                              jarFiles[
                                              ]
                                              validation-mode: AUTO
                                              shared-cache-mode: UNSPECIFIED
                                              properties[
                                                        hibernate.connection.autocommit: true
                                                        hibernate.dialect: org.hibernate.dialect.PostgreSQLDialect
                                                        hibernate.show_sql: true
                                                        hibernate.archive.autodetection: class
                                                        hibernate.format_sql: true
                                                        hibernate.hbm2ddl.auto: update
                                                        hibernate.transaction.factory_class: org.hibernate.transaction.JTATransactionFactory
                                              ]]
                                    13:48:36,088 TRACE [org.jboss.as.jpa] (MSC service thread 1-1) parsed persistence unit definitions for war elbapps.war
                                    13:48:36,089 TRACE [org.jboss.as.jpa] (MSC service thread 1-1) incrementing PU count for elbapps.war by 1
                                    13:48:36,144 TRACE [org.jboss.as.jpa] (MSC service thread 1-1) pu search for name 'ELBapps' inside of elbapps.war
                                    13:48:36,149 TRACE [org.jboss.as.jpa] (MSC service thread 1-1) findWithinDeployment check 'ELBapps' against pu 'ELBapps'
                                    13:48:36,151 TRACE [org.jboss.as.jpa] (MSC service thread 1-1) findWithinDeployment matched 'ELBapps' against pu 'ELBapps'
                                    13:48:36,152 TRACE [org.jboss.as.jpa] (MSC service thread 1-1) pu search found elbapps.war#ELBapps
                                    
                                    

                                     

                                    See this line:

                                     

                                    13:48:40,820 WARN  [org.hibernate.ejb.Ejb3Configuration] (MSC service thread 1-1) HHH000193: Overriding hibernate.transaction.factory_class is dangerous, this might break the EJB3 specification implementation
                                    

                                     

                                    Is this line relevant?

                                    1 2 Previous Next