0 Replies Latest reply on Feb 27, 2006 6:56 PM by Jochen Seifarth

    Inject EntityManager in JSF managed bean always NULL

    Jochen Seifarth Newbie

      Trying to inject an EntityManager into a JSF managed bean fails in JBoss 4.0.4RC1.

      My entities are in a JAR, JSF managed beans in a WAR (beans are listed as managed bean in faces-config.xml and work....apart from an injected EntityManager always being NULL). Both the JAR and the WAR are bundled in an EAR which is then deployed.

      META-INF/persistence.xml in entities JAR is

      <persistence>
       <persistence-unit name="jsEntities">
       <provider>org.hibernate.ejb.HibernatePersistence</provider>
       <jta-data-source>java:/bookingDatasource</jta-data-source>
       <properties>
       <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
       <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
       <property name="hibernate.transaction.flush_before_completion" value="true"/>
       <property name="hibernate.hbm2ddl.auto" value="update"/>
       <property name="hibernate.show_sql" value="true"/>
       </properties>
       </persistence-unit>
      </persistence>


      The JSF managed beans looks like this:

      import javax.annotation.Resource;
      import javax.persistence.*;
      import java.util.*;
      
      public class AddressPage {
       @PersistenceContext(unitName="jsEntities")
       private EntityManager em;
      
       @Resource
       private UserTransaction utx;
      
       public Person person = new Person();
      
       public void findPerson() {
       if (em == null) System.out.println(">>>em is null");
       person = em.find(Person.class, person.getGid());


      From a JSF page I use
      <h:inputText value="#{AddressPageBean.person.gid}" />
      <h:commandButton value="FindPerson" action="#{AddressPageBean.findPerson}" />


      which results in an the folllowing error in the log
      00:48:57,361 INFO [STDOUT] >>>em is null
      00:48:57,371 ERROR [[Faces Servlet]] Servlet.service() for servlet Faces Servlet threw exception
      javax.faces.FacesException: Error calling action method of component with id _id0:_id4
       at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:74)
       at javax.faces.component.UICommand.broadcast(UICommand.java:106)
       at javax.faces.component.UIViewRoot._broadcastForPhase(UIViewRoot.java:90)
       at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:164)
       at org.apache.myfaces.lifecycle.LifecycleImpl.invokeApplication(LifecycleImpl.java:316)
       at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:86)
       at javax.faces.webapp.FacesServlet.service(FacesServlet.java:106)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
       at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
       at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:54)
       at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:174)
       at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74)
       at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
       at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
       at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
       at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:868)
       at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:663)
       at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
       at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
       at java.lang.Thread.run(Unknown Source)
      Caused by: javax.faces.el.EvaluationException: Exception while invoking expression #{AddressPageBean.findPerson}
       at org.apache.myfaces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:153)
       at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:63)
       ... 25 more
      Caused by: java.lang.NullPointerException
       at info.agentbob.faces.AddressPage.findPerson(AddressPage.java:28)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
       at java.lang.reflect.Method.invoke(Unknown Source)
       at org.apache.myfaces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:129)
       ... 26 more
      


      Looks like the persistence context cannot be found and therefore the EntityManager is NULL, but why ?

      The persistence unit names match, therefore according to
      http://trailblazer.demo.jboss.com/EJB3Trail/persistence/config/ I don't need a jboss-app.xml in the EAR as I have specified the persistence unit name.

      What am I missing ?
      Can I not inject an EntityManager in a JSF managed bean ? Would a bean have to be a "proper" EJB3 bean rather than a JSF managed bean for the EntityManager injection to work ?

      Thanks for any ideas.