5 Replies Latest reply on Dec 8, 2011 8:16 AM by Jason Porter

    Persistence Context is not injected

    Andreas Doebeli Newbie

      I'm getting started to Seam 3 and Weld. Now I'm struggling with the perstistence - the entityManager is not being injected.


      Fist, I have set up a maven project from scratch (not generated). This project has two sub-modules:


      - model: just contains the entities  (packaged as jar)
      - web: contains front-end (jsf) and beans (no EJB). this module depends on the model module. (packaged as war)


      So in the web module, i have my persistence.xml in src/main/resources/META-INF:


          <?xml version="1.0" encoding="UTF-8"?>
               <!-- Persistence deployment descriptor for dev profile -->
          <persistence xmlns="http://java.sun.com/xml/ns/persistence"
                       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                       xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
                       version="2.0">
               
              <persistence-unit name="allternative" transaction-type="JTA">
                  <provider>org.hibernate.ejb.HibernatePersistence</provider>
                  <jta-data-source>java:jboss/datasources/allternativeDS</jta-data-source>
                  <properties>
                      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
                      <property name="hibernate.hbm2ddl.auto" value="update" />
                      <property name="hibernate.show_sql" value="false" />
                      <property name="hibernate.format_sql" value="true" />
                      <property name="hibernate.transaction.manager_lookup_class"
                              value="org.hibernate.transaction.JBossTransactionManagerLookup" />
                      <property name="hibernate.connection.autocommit" value="true" />
                  </properties>
              </persistence-unit>
              
          </persistence>



      then I have the class ApplicationInitializer:


          @Named
          @ApplicationScoped
          public class ApplicationInitializer {
               
               @Produces
               @PersistenceContext(unitName="allternative")
               @Dependent
               EntityManager entityManager;
               
               @Produces
               @PersistenceUnit(unitName="allternative")
               @Dependent
               EntityManagerFactory entityManagerFactory;
          
          }



      And the class CurrentUser:


          @Named("currentUser")
          @SessionScoped
          public class CurrentUser implements Serializable {
          
               private boolean isLogin = false;
               
               @Inject
               private EntityManager entityManager;
               
               @Inject
               private Logger log;
               
               private User user;
               
               public CurrentUser() {
                    this.user = null;
               }
          
               public User getUser() {
                    log.info("getting user");
                    if(this.user == null) {
                         log.info("user is null, getting default from db");
                         this.user = entityManager.find(User.class, "thobens"); // <- strange exception here
          //               log.info("took user "+user.getUsername());
                    }
                    return user;
               }
          
               public void setUser(User user) {
                    this.user = user;
               }
          }



      when i call #{currentUser.user} I get the following strange exception:




          16:57:04,768 SEVERE [javax.enterprise.resource.webcontainer.jsf.application] (http--127.0.0.1-8080-1) Error Rendering View[/index.xhtml]: javax.el.ELException: /index.xhtml: java.lang.RuntimeException: Can't find a deployment unit named allternative at deployment "web.war"
               at com.sun.faces.facelets.compiler.TextInstruction.write(TextInstruction.java:88) [jsf-impl-2.1.3-b02-jbossorg-2.jar:2.1.3-SNAPSHOT]
               at com.sun.faces.facelets.compiler.UIInstructions.encodeBegin(UIInstructions.java:82) [jsf-impl-2.1.3-b02-jbossorg-2.jar:2.1.3-SNAPSHOT]
               at com.sun.faces.facelets.compiler.UILeaf.encodeAll(UILeaf.java:183) [jsf-impl-2.1.3-b02-jbossorg-2.jar:2.1.3-SNAPSHOT]
               at org.richfaces.renderkit.RendererBase.renderChildren(RendererBase.java:278) [richfaces-components-ui-4.0.0.Final.jar:]
               at org.richfaces.renderkit.html.PanelRenderer.encodeEnd(PanelRenderer.java:181) [richfaces-components-ui-4.0.0.Final.jar:]
               at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1]
               at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1763) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1]
               at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1759) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1]
               at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1759) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1]
               at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:401) [jsf-impl-2.1.3-b02-jbossorg-2.jar:2.1.3-SNAPSHOT]
               at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131) [jsf-impl-2.1.3-b02-jbossorg-2.jar:2.1.3-SNAPSHOT]
               at com.ocpsoft.pretty.faces.application.PrettyViewHandler.renderView(PrettyViewHandler.java:163) [prettyfaces-jsf2-3.3.0.jar:]
               at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:288) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1]
               at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:288) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1]
               at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:288) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1]
               at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121) [jsf-impl-2.1.3-b02-jbossorg-2.jar:2.1.3-SNAPSHOT]
               at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) [jsf-impl-2.1.3-b02-jbossorg-2.jar:2.1.3-SNAPSHOT]
               at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139) [jsf-impl-2.1.3-b02-jbossorg-2.jar:2.1.3-SNAPSHOT]
               at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1]
               at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
               at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
               at org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:67) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]
               at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
               at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
               at com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.java:118) [prettyfaces-jsf2-3.3.0.jar:]
               at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
               at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
               at org.jboss.solder.servlet.exception.CatchExceptionFilter.doFilter(CatchExceptionFilter.java:65) [solder-impl-3.1.0.Beta3.jar:]
               at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
               at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
               at org.jboss.solder.servlet.event.ServletEventBridgeFilter.doFilter(ServletEventBridgeFilter.java:72) [solder-impl-3.1.0.Beta3.jar:]
               at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
               at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
               at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
               at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
               at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:139) [jboss-as-web-7.0.2.Final.jar:7.0.2.Final]
               at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:388) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
               at org.jboss.as.web.NamingValve.invoke(NamingValve.java:57) [jboss-as-web-7.0.2.Final.jar:7.0.2.Final]
               at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:49) [jboss-as-jpa-7.0.2.Final.jar:7.0.2.Final]
               at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:154) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
               at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
               at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
               at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
               at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
               at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:667) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
               at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:952) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
               at java.lang.Thread.run(Thread.java:619) [:1.6.0_20]
          Caused by: java.lang.RuntimeException: Can't find a deployment unit named allternative at deployment "web.war"
               at org.jboss.as.weld.services.bootstrap.WeldJpaInjectionServices.getScopedPUName(WeldJpaInjectionServices.java:94) [jboss-as-weld-7.0.2.Final.jar:7.0.2.Final]
               at org.jboss.as.weld.services.bootstrap.WeldJpaInjectionServices.resolvePersistenceContext(WeldJpaInjectionServices.java:59) [jboss-as-weld-7.0.2.Final.jar:7.0.2.Final]
               at org.jboss.weld.util.Beans.injectEEFields(Beans.java:784) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]
               at org.jboss.weld.bean.ManagedBean$ManagedBeanInjectionTarget$1$1.proceed(ManagedBean.java:181) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]
               at org.jboss.weld.injection.InjectionContextImpl.run(InjectionContextImpl.java:54) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]
               at org.jboss.weld.bean.ManagedBean$ManagedBeanInjectionTarget$1.work(ManagedBean.java:176) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]
               at org.jboss.weld.bean.ManagedBean$FixInjectionPoint.run(ManagedBean.java:142) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]
               at org.jboss.weld.bean.ManagedBean$ManagedBeanInjectionTarget.inject(ManagedBean.java:170) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]
               at org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:339) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]
               at org.jboss.weld.context.AbstractContext.get(AbstractContext.java:122) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]
               at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:693) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]
               at org.jboss.weld.bean.AbstractReceiverBean.getReceiver(AbstractReceiverBean.java:84) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]
               at org.jboss.weld.bean.ProducerField$1.produce(ProducerField.java:134) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]
               at org.jboss.weld.bean.AbstractProducerBean.create(AbstractProducerBean.java:361) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]
               at org.jboss.weld.bean.builtin.ee.EEResourceProducerField.createUnderlying(EEResourceProducerField.java:170) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]
               at org.jboss.weld.bean.builtin.ee.EEResourceProducerField.access$000(EEResourceProducerField.java:54) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]
               at org.jboss.weld.bean.builtin.ee.EEResourceProducerField$EEResourceCallable.call(EEResourceProducerField.java:80) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]
               at org.jboss.weld.bean.builtin.CallableMethodHandler.invoke(CallableMethodHandler.java:50) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]
               at org.jboss.weld.bean.proxy.EnterpriseTargetBeanInstance.invoke(EnterpriseTargetBeanInstance.java:62) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]
               at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:125) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]
               at org.jboss.weldx.persistence.EntityManager$-1772293428$Proxy$_$$_Weld$Proxy$.find(EntityManager$-1772293428$Proxy$_$$_Weld$Proxy$.java) [weld-core-1.1.2.Final.jar:]
               at com.allternative.web.security.CurrentUser.getUser(CurrentUser.java:42) [classes:]
               at com.allternative.web.security.CurrentUser$Proxy$_$$_WeldClientProxy.getUser(CurrentUser$Proxy$_$$_WeldClientProxy.java) [classes:]
               at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.6.0_20]
               at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [:1.6.0_20]
               at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [:1.6.0_20]
               at java.lang.reflect.Method.invoke(Method.java:597) [:1.6.0_20]
               at javax.el.BeanELResolver.getValue(BeanELResolver.java:302) [jboss-el-api_2.2_spec-1.0.0.Final.jar:1.0.0.Final]
               at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176) [jsf-impl-2.1.3-b02-jbossorg-2.jar:2.1.3-SNAPSHOT]
               at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203) [jsf-impl-2.1.3-b02-jbossorg-2.jar:2.1.3-SNAPSHOT]
               at org.apache.el.parser.AstValue.getValue(AstValue.java:134) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
               at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:187) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
               at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:55) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]
               at com.sun.faces.facelets.el.ELText$ELTextVariable.writeText(ELText.java:224) [jsf-impl-2.1.3-b02-jbossorg-2.jar:2.1.3-SNAPSHOT]
               at com.sun.faces.facelets.el.ELText$ELTextComposite.writeText(ELText.java:148) [jsf-impl-2.1.3-b02-jbossorg-2.jar:2.1.3-SNAPSHOT]
               at com.sun.faces.facelets.compiler.TextInstruction.write(TextInstruction.java:85) [jsf-impl-2.1.3-b02-jbossorg-2.jar:2.1.3-SNAPSHOT]
               ... 46 more




      This occurs as soon i touch entityManager. When I annotate it with @PersistenceContext instead of @Inject this error occurs already at deployment.


      I guess it has something to do with the persistence.xml not beeing processed, because


      a) When I call Persistence.createEntityManagerFactory("allternative").createEntityManager(), I get an error saying that there is no such persistence unit.


      b) I have hibernate.hbm2ddl.auto set to update (and also tried with create), but I'm getting no feedback that my entities are created in the DB (and running show tables; on mysql also results an empty set). I also don't get a hint if the persistence unit is created or not. The DataSource is available (and deployed by JBoss) and the db is created.


      My beans.xml is locatex at src/main/webapp/WEB-INF/ (only in the web module):


          <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns="http://java.sun.com/xml/ns/javaee" 
               xmlns:t="urn:java:org.jboss.seam.persistence.transaction"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="
                http://java.sun.com/xml/ns/javaee   
                http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
               <interceptors>
                    <class>org.jboss.seam.faces.context.conversation.ConversationBoundaryInterceptor</class>
                    <class>org.jboss.seam.transaction.TransactionInterceptor</class>
               </interceptors>
               
          </beans>



      and currently I have one entity (in the model module):


         @Entity
          @Table(name="user")
          public class User {
               
               @Id
               @Length(min=6, max=32)
               private String username;
          
               @NotNull
               private String firstname;
               
               @NotNull
               private String lastname;
               
               @NotNull
               @Length(min=6, max=64)
               private String password;
          
               public String getUsername() {
                    return username;
               }
          
               public void setUsername(String user) {
                    this.username = user;
               }
          
               public String getFirstname() {
                    return firstname;
               }
          
               public void setFirstname(String firstname) {
                    this.firstname = firstname;
               }
          
               public String getLastname() {
                    return lastname;
               }
          
               public void setLastname(String lastname) {
                    this.lastname = lastname;
               }
          
               public String getPassword() {
                    return password;
               }
          
               public void setPassword(String password) {
                    this.password = password;
               }
               
               @Override
               public int hashCode() {
                    return this.username.hashCode();
               }
               
               @Override
               public boolean equals(Object other) {
                    if(other == null) {
                         return false;
                    }
                    if(!(other instanceof User)) {
                         return false;
                    }
                    User otherUser = (User)other;
                    String user = otherUser.getUsername();
                    if(user == null || this.username == null) {
                         return false;
                    }
                    return this.username.equals(user);
               }
               
               @Override
               public String toString() {
                    return this.username;
               }
          }



      I also noticed the following log entries form JBoss AS7:


      15:23:55,218 INFO  [org.jboss.solder.core.CoreExtension] (MSC service thread 1-8) Preventing class org.jboss.seam.persistence.HibernatePersistenceProvider from being installed as bean due to @Veto annotation
      15:23:55,233 INFO  [org.jboss.solder.core.CoreExtension] (MSC service thread 1-8) Preventing class org.jboss.seam.persistence.hibernate.HibernateManagedSessionExtensionImpl from being installed as bean due to @Veto annotation




      Am I missing a dependency here? I only have the seam-persistence module as dependency regarding jpa / persistence in my pom.xml. I Actually tried several additional dependencies like hibernate-jpamodelgen, hibernate-entitymanager, hibernate-core etc. but that did not take any effect.


      I really don't get why this does not work. Can anyone help me, please?


      Thanks in advance,


      Andreas