5 Replies Latest reply on Aug 21, 2006 5:18 PM by Eric Ray

    EntityManager not being injected

    Eric Ray Novice

      I have a situation where I need an EM injected into a JSF Converter implementation class and I can't seem to make that happen. All I need to do is take the string value passed in to the getAsObject method and retrieve the appropriate database object.

      At first I thought I needed to make the converter class a SLSB but when I tried that it didn't work either.

      Any thoughts.
      Thanks.
      Eric Ray

        • 1. Re: EntityManager not being injected
          None None Master

          Hmm. Do you have a persistence context defined in your jar file?

          • 2. Re: EntityManager not being injected
            Eric Ray Novice

            Yes. I'm injecting the EM all over the place. All that works great. It's just that the Converter implementation doesn't get it injected. I suspect it has something to do with being created outside of the CMP context. But I really have no idea.

            I'm still looking.

            BTW, I've resovled to create my own like so...

             EntityManagerFactory emf = Persistence.createEntityManagerFactory("DC");
             EntityManager em = emf.createEntityManager();
            


            but that gives me the following error
            14:31:36,828 ERROR [[Faces Servlet]] Servlet.service() for servlet Faces Servlet threw exception
            java.lang.NoClassDefFoundError: net/sf/ehcache/CacheException
             at java.lang.Class.getDeclaredConstructors0(Native Method)
             at java.lang.Class.privateGetDeclaredConstructors(Class.java:2328)
             at java.lang.Class.getConstructor0(Class.java:2640)
             at java.lang.Class.newInstance0(Class.java:321)
             at java.lang.Class.newInstance(Class.java:303)
             at org.hibernate.cfg.SettingsFactory.createCacheProvider(SettingsFactory.java:346)
             at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:220)
             at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:1928)
             at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1211)
             at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:631)
             at org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:760)
             at org.hibernate.ejb.Ejb3Configuration.createFactory(Ejb3Configuration.java:151)
             at org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:205)
             at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:114)
             at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:37)
             at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:27)
             at com.vicor.distributedcapture.session.CodeEntityConverter.getAsObject(CodeEntityConverter.java:19)
             at org.apache.myfaces.renderkit.RendererUtils.getConvertedUIOutputValue(RendererUtils.java:658)
             at org.apache.myfaces.renderkit.html.HtmlMenuRendererBase.getConvertedValue(HtmlMenuRendererBase.java:111)
             at javax.faces.component.UIInput.getConvertedValue(UIInput.java:289)
             at javax.faces.component.UIInput.validate(UIInput.java:265)
             at javax.faces.component.UIInput.processValidators(UIInput.java:144)
             at javax.faces.component.UIData.process(UIData.java:514)
             at javax.faces.component.UIData.processColumnChildren(UIData.java:498)
             at javax.faces.component.UIData.processValidators(UIData.java:403)
             at javax.faces.component.UIForm.processValidators(UIForm.java:68)
             at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:417)
             at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:142)
             at org.apache.myfaces.lifecycle.LifecycleImpl.processValidations(LifecycleImpl.java:240)
             at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:76)
             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.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175)
             at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:524)
             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:869)
             at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
             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(Thread.java:595)
            


            • 3. Re: EntityManager not being injected
              Eric Ray Novice

              Here's the code

              jsf code...

               <h:selectOneMenu value="#{elem.code}" >
               <f:selectItems value="#{element.codeMap}" />
               </h:selectOneMenu>
              


              Converter implementation

               @PersistenceContext(unitName="DC")
               EntityManager em;
              
               public Object getAsObject(FacesContext fContext, UIComponent component, String value)
               throws ConverterException {
               CodeEntity ce = em.find(CodeEntity.class, value);
               return ce;
               }
              


              em is always null. How can that be? This class is inside the container. Even if I make it a Session Bean is still doesn't work.

              Need ideas. Thanks.


              • 4. Re: EntityManager not being injected
                Patrick Angeles Novice

                The problem is that the Converter object as you have it is instantiated by JSF, not by Seam, and so no bijection takes place.

                You can try retrieving the EntityManager yourself via the FacesContext. Or, have a look at the following threads that propose complete solutions:

                http://www.jboss.com/index.html?module=bb&op=viewtopic&t=71182
                http://www.jboss.com/index.html?module=bb&op=viewtopic&t=75802
                http://www.jboss.org/index.html?module=bb&op=viewtopic&p=3925444

                • 5. Re: EntityManager not being injected
                  Eric Ray Novice

                  True, but @PersistenceContext is not a Seam annotation. It's an EJB3 annotation that should be injected by the EJB3 container. Or so I thought.

                  I guess I still have a lot to learn about this stuff.