1 2 Previous Next 20 Replies Latest reply on Oct 18, 2011 2:12 PM by redmond007 Go to original post
      • 15. Re: @PersistenceUnit not working
        smarlow
        
        Message: Failed to add resource root 'hibernate-entitymanager-4.0.0-SNAPSHOT' at path 'hibernate-entitymanager-4.0.0-SNAPSHOT'
        

         

        Sounds like your missing the ".jar" extension (should be looking for hibernate-entitymanager-4.0.0-SNAPSHOT.jar I think).

        • 16. Re: @PersistenceUnit not working
          redmond007

          Thanks, I was able to fix module.xml.  Looks like I've gotten past the dom4j problem, but I'm still experiencing deploy errors, it still looks like my code is still generating a NPE at this point in the code

           

           

          @PersistenceUnit(unitName="primary")
          
          
              private static EntityManagerFactory entityManagerFactory;        
              EntityManager entityManager = entityManagerFactory.createEntityManager();
          
          

           

           

          My persistence.xml again is the same as above in this thread.  The server logs look like

           

          13:22:17,076 INFO  [javax.enterprise.resource.webcontainer.jsf.config] (MSC service thread 1-14) Initializing Mojarra 2.1.3 (SNAPSHOT 20110825) for context '/myapp'

          13:22:53,348 ERROR [stderr] (MSC service thread 1-14) java.lang.NullPointerException

          13:22:53,368 ERROR [stderr] (MSC service thread 1-14)   at com.mycompany.myapp.common.persistence.HibernateUtil.currentSession(HibernateUtil.java:75)

          13:22:53,368 ERROR [stderr] (MSC service thread 1-14)   at com.mycompany.myapp.common.businessobjects.ServerSettings.GetServerSettings(ServerSettings.java:225)

          13:22:53,369 ERROR [stderr] (MSC service thread 1-14)   at com.mycompany.myapp.common.servlet.SecurityFilter.init(SecurityFilter.java:55)

          13:22:53,369 ERROR [stderr] (MSC service thread 1-14)   at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:447)

          13:22:53,369 ERROR [stderr] (MSC service thread 1-14)   at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3269)

          13:22:53,369 ERROR [stderr] (MSC service thread 1-14)   at org.apache.catalina.core.StandardContext.start(StandardContext.java:3860)

          13:22:53,369 ERROR [stderr] (MSC service thread 1-14)   at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:81)

          13:22:53,369 ERROR [stderr] (MSC service thread 1-14)   at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1824)

          13:22:53,369 ERROR [stderr] (MSC service thread 1-14)   at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1759)

          13:22:53,370 ERROR [stderr] (MSC service thread 1-14)   at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)

          13:22:53,370 ERROR [stderr] (MSC service thread 1-14)   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)

          13:22:53,370 ERROR [stderr] (MSC service thread 1-14)   at java.lang.Thread.run(Thread.java:680)

          ~                                                                                              

          ~                                                      

           

           

          I might be missing something - any suggestions?

          • 17. Re: @PersistenceUnit not working
            smarlow

            Hmm, your trying to inject into a servlet filter.  I wonder if its a timing issue, in that the filter is initialized before the EntityManagerFactory is injected.  I also wonder if the EntityManagerFactory ever gets injected.

             

            As a test, leave the EntityManagerFactory where it is but move the EntityManager variable/initialization code inside your doFilter method (along with any code that currently uses the EntityManager).

             

            Let us know if you then get a NPE inside the doFilter, when accessing the entityManagerFactory.  If you still get a NPE, its a bug and creating a jira will be the next step.  The following spec sections can be referenced in the bug report (if you have to create one).

             

            The JPA 2.0 specification section 7.1 states the following:

             

            Both container-managed entity managers and application-managed entity managers and their persistence contexts are required to be supported in Java EE web containers and EJB containers. Within an EJB environment, container-managed entity managers are typically used.

             

            JPA 2.0 specification section 7.8.2 states:

             

            The container is responsible for managing the lifecycle of container-managed persistence contexts, for injecting EntityManager references into web components and session bean and message-driven bean components, and for making EntityManager references available to direct lookups in JNDI.

             

            The EE 6.0 platform specification section EE.4.2.1 (Web Components) states:

             

            Servlets and JSP pages demarcate a transaction using the javax.transaction.UserTransaction interface which is defined in the JTA specification. They may access multiple resource managers and invoke multiple enterprise beans within a single transaction. The specified transaction context is automatically propagated to the enterprise beans and transactional resource managers. The result of the propagation may be subject to the enterprise bean transaction attributes (for example, a bean may be required to use Container Managed Transactions).

             

            Servlet filters and web application event listeners must not demarcate transactions using the javax.transaction.UserTransaction interface. Servlet filters may use transactional resources in a local transaction mode within their doFilter methods but should not use any transactional resources in the methods of any objects used to wrap the request or response objects.

            • 18. Re: @PersistenceUnit not working
              redmond007

              As a test, leave the EntityManagerFactory where it is but move the EntityManager variable/initialization code inside your doFilter method (along with any code that currently uses the EntityManager).

               

              --We were actually receiving the NPE in our SecurityFilter class's init method.  The SecurityFilter is initialized during the deploy process in order to initialize certain system properties.  The way it is done in our application is: SecurityFilter class initializes an instance of the ServerSettings class, which uses HibernateUtil to obtain an  EntityManagerFactory, an EntityManager and a hibernate Session object in order to execute a hibernate query in the ServerSettings class, and return the results of that query to the SecurityFilter init() method.  Afaik SecurityFilter.doFilter() is not called during application deployment.

               

               

              I can however, create  EntityManagerFactory and EntityManager within SecurityFilter's init() method in order to ascertain whether this is truly a bug or not.  I modified my SecurityFilter class so that it creates  EntityManagerFactory and EntityManager.  The code looks like this right now

               

               

              public class SecurityFilter implements Filter {
              
                        private String loginPage = "";
                        private String noAccessPage = "";
                        private ArrayList secureSearch;
                        private Properties securityProperties;
                  
                        @PersistenceUnit(unitName="primary")
                  private static EntityManagerFactory entityManagerFactory;
                
                  public void init(FilterConfig config) throws ServletException {
                            this.loginPage = config.getInitParameter("loginPage");
                            this.noAccessPage = config.getInitParameter("noAccessPage");
                            if (config.getInitParameter("secureSearch") == null)
                            {
                                      secureSearch = null;
                            } else
                            {
                                      secureSearch = new ArrayList();
                                      String rolesString = config.getInitParameter("secureSearch");
                                      String[] rolesArray = rolesString.split(",");
                                      for (int i = 0; i < rolesArray.length; ++i)
                                      {
                                                secureSearch.add(rolesArray[i]);
                                      }
                            }
                            securityProperties = null;
                            try
                                  {
                  
                                EntityManager entityManager = entityManagerFactory.createEntityManager();
              
              

              After running this code through a debugger, I've found that EntityManager is not null.  So if creation of EntityManagerFactory and EntityManager were confined to the SecurityFilter class, and not spread out across several classes, I wouldn't be receiving a NPE during initializing of SecurityFilter during deploy.

               

               

              Please let me know if you need any more information from me.  I can see now that probably what needs to happen is reorganization of my code so that EMF and EM are available at this point in the deploy.  As you can see from my persistence.xml upthread - I'm using a non-jta, application managed data source - would moving towards a jta, container managed data source help with this issue?

               

               

              Thank you again.

              • 19. Re: @PersistenceUnit not working
                smarlow

                I was just trying to understand if the injection into the SecurityFilter.entityManagerFactory was occuring or not.  From what you said before and above.  It seemed questionable. 

                 

                I think we talked elsewhere about a problem with AS7 not loading the Hibernate provider until it is needed (which will be fixed for the AS 7.1 release to be initialized during AS7 server start).  Are you  testing with a recent AS7 nightly build or when HibernateUtil was being used and you got the NPE?

                • 20. Re: @PersistenceUnit not working
                  redmond007

                  Yes, the @PersistenceUnit injection, and creation of EMF and EM inside of SecurityFilter is working, it doesn't seem to be working when SecurityFilter initiates the injection of @PersistenceUnit and creation of EMF in another class during application deployment.  This appears to be some sort of timing issue, although I've tried things such as upping our deployment timeout to 360 seconds, which doesn't help.

                   

                  http://community.jboss.org/thread/172498

                  Yes in this thread, we discovered the provider was not being loaded at server start.  I was testing with AS7 nightly build from the end of last week. 

                  1 2 Previous Next