1 2 Previous Next 18 Replies Latest reply on Sep 22, 2007 5:03 PM by pmuir

    access the entityManager in a converter (seam 2 beta)

    adrien.loyat

      Hello
      In seam 1.3 alpha, I use the entityManager in a converter (in getAsObject) like this :


      public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
       if (StringUtils.isEmpty(arg2)){ return null;}
       log.info("getAsObject avec arguement #0", arg2);
      
       TypecolList typecolList = (TypecolList) Component.getInstance("typecolList");
       if (typecolList == null ) {
       log.info("typecolList est null");
       return null;
       }
       EntityManager em = typecolList.getEntityManager();
       Typecol typecol = em.find(Typecol.class, Integer.parseInt(arg2) );
      
       return typecol;
       }
      

      I am a newbie and I know this code is not clean.

      With seam 2 beta, It raises an exception: EntityManager is closed.

      So my question is : how can I access to my entities without using the EntityManeager ? or How can I have access to the entity Manager ?

      Best Regards
      Adrien

        • 1. Re: access the entityManager in a converter (seam 2 beta)
          dajevtic

          I am not sure if this is the best way. It would probably be better to use the seam entity converter, but here goes:

           InitialContext ctx = new InitialContext();
           String jndiEntityManagerFactory = "factoryDefinedInComponentsXml";
           EntityManagerFactory factory = (EntityManagerFactory) ctx
           .lookup(jndiEntityManagerFactory);
           EntityManager entityManager = factory.createEntityManager();
          


          Hope, it helps!

          • 2. Re: access the entityManager in a converter (seam 2 beta)
            adrien.loyat

            Ousp

            The exception is not in my converter. It is the <s:convertEntity /> in my xhtml page that raises it !

            • 3. Re: access the entityManager in a converter (seam 2 beta)
              pmuir

              Easiest way is to use a Seam component

              @Name("myConverter") @BypassInterceptors @Converter
              public class MyConverter implements Converter{
              
               @In EntityManager entityManager;
              
               @Transactional
               public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
               entityManager.joinTransaction();
               ...
               }
              }


              <h:selectOneMenu ... converter="myConverter">


              • 4. Re: access the entityManager in a converter (seam 2 beta)
                dajevtic

                do you mind posting the snipped of your page?

                • 5. Re: access the entityManager in a converter (seam 2 beta)
                  adrien.loyat

                   

                  <s:decorate template="layout/display.xhtml">
                   <ui:define name="label">Type de collectivité</ui:define>
                   <h:selectOneMenu id="typecol" value="#{colList.col.typecol}" >
                   <s:selectItems value="#{typecolList.resultList}" var="untypcol" label="#{untypcol.nom}" noSelectionLabel="Select..." hideNoSelectionLabel="false"/>
                   <s:convertEntity />
                   </h:selectOneMenu>
                   </s:decorate>
                  


                  I'am looking at the doc references to see if something has changed.

                  • 6. Re: access the entityManager in a converter (seam 2 beta)
                    adrien.loyat

                    I dont't undestand the problem with s:convertEntity.


                    Description

                    Assigns an entity converter to the current component. This is primarily useful for radio button and dropdown controls.

                    The converter works with any managed entity which has an @Id annotation - either simple or composite.

                    Attributes

                    None.

                    Configuration

                    You must use Seam managed transactions (see Section 8.2, ?Seam managed transactions?) with <s:convertEntity />.


                    There is an Id on typeCol.
                    Seam managed transactions is the default behavior and I didn't change anything about this.




                    • 7. Re: access the entityManager in a converter (seam 2 beta)
                      dajevtic

                      It's hard for me to understand, too, especially when I don't get that exception.
                      Post the complete stack trace.
                      Probably colList and typeColList are retrieved in seperate transactions, which causes this problem...

                      • 8. Re: access the entityManager in a converter (seam 2 beta)
                        adrien.loyat

                        the stack trace :

                        Exception during request processing:
                        Caused by javax.servlet.ServletException with message: "EntityManager is closed"
                        javax.faces.webapp.FacesServlet.service(FacesServlet.java:256)
                        org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
                        org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                        org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
                        org.jboss.seam.debug.hot.HotDeployFilter.doFilter(HotDeployFilter.java:63)
                        org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                        org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:87)
                        org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                        org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:63)
                        org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                        org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:46)
                        org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                        org.ajax4jsf.framework.ajax.xmlfilter.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:127)
                        org.ajax4jsf.framework.ajax.xmlfilter.BaseFilter.doFilter(BaseFilter.java:277)
                        org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:40)
                        org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                        org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:140)
                        org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                        org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                        org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
                        org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                        org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                        org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
                        org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
                        org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
                        org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:433)
                        org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
                        org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
                        org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
                        org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:156)
                        org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
                        org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:241)
                        org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
                        org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:580)
                        org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
                        java.lang.Thread.run(Thread.java:595)
                        
                        Caused by java.lang.IllegalStateException with message: "EntityManager is closed"
                        
                        org.hibernate.ejb.EntityManagerImpl.getSession(EntityManagerImpl.java:42)
                        org.hibernate.ejb.AbstractEntityManagerImpl.joinTransaction(AbstractEntityManagerImpl.java:447)
                        org.hibernate.ejb.AbstractEntityManagerImpl.joinTransaction(AbstractEntityManagerImpl.java:442)
                        org.jboss.seam.persistence.EntityManagerProxy.joinTransaction(EntityManagerProxy.java:120)
                        org.jboss.seam.framework.EntityIdentifier.find(EntityIdentifier.java:25)
                        org.jboss.seam.ui.converter.EntityConverterStore.get(EntityConverterStore.java:43)
                        org.jboss.seam.ui.converter.EntityConverter.getAsObject(EntityConverter.java:81)
                        org.jboss.seam.ui.converter.PrioritizableConverter.getAsObject(PrioritizableConverter.java:61)
                        org.jboss.seam.ui.converter.ConverterChain.getAsObject(ConverterChain.java:107)
                        com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:152)
                        com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectOneValue(MenuRenderer.java:197)
                        com.sun.faces.renderkit.html_basic.MenuRenderer.getConvertedValue(MenuRenderer.java:359)
                        javax.faces.component.UIInput.getConvertedValue(UIInput.java:934)
                        javax.faces.component.UIInput.validate(UIInput.java:860)
                        javax.faces.component.UIInput.executeValidate(UIInput.java:1065)
                        javax.faces.component.UIInput.processValidators(UIInput.java:666)
                        javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1030)
                        javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1030)
                        javax.faces.component.UIForm.processValidators(UIForm.java:229)
                        javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1030)
                        javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:662)
                        org.ajax4jsf.framework.ajax.AjaxViewRoot.access$201(AjaxViewRoot.java:53)
                        org.ajax4jsf.framework.ajax.AjaxViewRoot$3.invokeRoot(AjaxViewRoot.java:315)
                        org.ajax4jsf.framework.ajax.JsfOneOneInvoker.invokeOnRegionOrRoot(JsfOneOneInvoker.java:53)
                        org.ajax4jsf.framework.ajax.AjaxContext.invokeOnRegionOrRoot(AjaxContext.java:191)
                        org.ajax4jsf.framework.ajax.AjaxViewRoot.processValidators(AjaxViewRoot.java:329)
                        com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:100)
                        com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
                        com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
                        javax.faces.webapp.FacesServlet.service(FacesServlet.java:244)
                        org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
                        org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                        org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
                        org.jboss.seam.debug.hot.HotDeployFilter.doFilter(HotDeployFilter.java:63)
                        org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                        org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:87)
                        org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                        org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:63)
                        org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                        org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:46)
                        org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                        org.ajax4jsf.framework.ajax.xmlfilter.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:127)
                        org.ajax4jsf.framework.ajax.xmlfilter.BaseFilter.doFilter(BaseFilter.java:277)
                        org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:40)
                        org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                        org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:140)
                        org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                        org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                        org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
                        org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                        org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                        org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
                        org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
                        org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
                        org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:433)
                        org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
                        org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
                        org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
                        org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:156)
                        org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
                        org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:241)
                        org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
                        org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:580)
                        org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
                        java.lang.Thread.run(Thread.java:595)


                        • 9. Re: access the entityManager in a converter (seam 2 beta)
                          adrien.loyat

                          To add a few more words : I have the same generated code by seam-gen with seam 1.3alpha and seam 2beta. But the convertEntity just doesn't work with the seam 2beta version.

                          Thanks for your help.

                          • 10. Re: access the entityManager in a converter (seam 2 beta)
                            dajevtic

                            Sorry, really don't know. Have you followed all the steps porting to 2.0, like eliminate Ajax4JSF Filter in web.xml, remove the (Tranactional)SeamPhaseListener declaration, etc. etc. etc.

                            I know that there are some open issues since Pete's change of the entity converter, might also be related to the downgrade of the entity converter functionality...

                            • 11. Re: access the entityManager in a converter (seam 2 beta)
                              pmuir

                              So, the transaction isn't active at that point. Describe how you get to this page etc. See if we can work out why you don't have a transaction.

                              • 12. Re: access the entityManager in a converter (seam 2 beta)
                                dajevtic

                                Pete,
                                my guess is that after the page the conversation (or what ever scope) is not active any more.
                                I tried it out and the only time I can reproduce is if the scope has ended after the page is displayed.
                                When a value is selected and the selection submitted, then this stack trace occurs.
                                Hope it helps you find a solution.

                                • 13. Re: access the entityManager in a converter (seam 2 beta)
                                  pmuir

                                  If you can submit a simple test case to JIRA I can look at it.

                                  • 14. Re: access the entityManager in a converter (seam 2 beta)
                                    adrien.loyat

                                    Sorry I've had to leave my office yesterday.

                                    Have you followed all the steps porting to 2.0, like eliminate Ajax4JSF Filter in web.xml,


                                    I used seam-gen (setup then generate entities) to create from nothing.

                                    If you can submit a simple test case to JIRA I can look at it.

                                    Ok. I Try to do this.

                                    I'm not the only one facing this issue : http://www.jboss.com/index.html?module=bb&op=viewtopic&t=112236

                                    Thanks for your help.

                                    1 2 Previous Next