1 2 Previous Next 17 Replies Latest reply on Jul 17, 2008 11:56 PM by bashan

    s:convertEntity problem

    bashan
      Hi,
      
      I am trying to use: <s:convertEntity>
       but getting this exception:
      Caused by: java.lang.IllegalStateException: Unable to access a persistence context. 
      You must either have a SMPC called entityManager or configure one in components.xml
      
      I understand that I have to add the following configuration that things will work:
      <component name="org.jboss.seam.ui.entityConverter">
        <property name="entityManager">#{name of entity manager}</property>
      </component>
      
      The problem is that I don't have any entityManager defined but 
      only Hibernate managed session (is it correct? Should I also have entityManager?). 
      This is the relevant part of my component.xml:
      
        <persistence:hibernate-session-factory name="hibernateSessionFactory"/>
      
        <persistence:managed-hibernate-session name="myDatabase"
                                               session-factory="#{hibernateSessionFactory}"
                                               auto-create="true"/>
      
        <transaction:hibernate-transaction session="#{myDatabase}"/>
      
      Can I use the <s:convertEntity> with this kind of configuration? 
      Must I add entityManager? 
      I never managed to configure the entityManager to work (I am using tomcat 6 and don't want to install JBoss EJB at this point).
      



      Sorry for writing in a code block, this is the only way I can post a message without enless fights with Seam wiki engine ...


      Thanks,
      Guy.





        • 1. Re: s:convertEntity problem
          gjeudy

          I don't think you need to add a component for entityConverter in the components.xml. It is being installed by default, checkout the source code:


          @Name("org.jboss.seam.ui.EntityConverter")
          @Scope(CONVERSATION)
          @Install(precedence = BUILT_IN)
          @Converter
          @BypassInterceptors
          public class EntityConverter implements
                   javax.faces.convert.Converter, Serializable
          



          I use s:convertEntity with SMPC (Seam managed persistence context) config in components.xml:


          <persistence:managed-persistence-context name="entityManager" auto-create="true"
                    persistence-unit-jndi-name="java:/rdmEntityManagerFactory" />
          



          I don't know if you can use hibernate session with it, I suggest you try it.

          • 2. Re: s:convertEntity problem
            mtorres

            You can try, this seem to work for me.


            <component name="org.jboss.seam.ui.EntityConverter">
                        <property name="session">#{myDatabase}</property>
            </component>


            • 3. Re: s:convertEntity problem
              bashan

              Thanks, I tried that and god this exception:


              java.lang.RuntimeException: Could not create Component: org.jboss.seam.ui.EntityConverter
                   at org.jboss.seam.init.Initialization.addComponent(Initialization.java:1032)
                   at org.jboss.seam.init.Initialization.installComponents(Initialization.java:948)
                   at org.jboss.seam.init.Initialization.init(Initialization.java:623)
                   at org.jboss.seam.servlet.SeamListener.contextInitialized(SeamListener.java:34)
                   at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3843)
                   at org.apache.catalina.core.StandardContext.start(StandardContext.java:4350)
                   at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
                   at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
                   at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)
                   at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:626)
                   at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:553)
                   at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:488)
                   at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1147)
                   at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
                   at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
                   at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
                   at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
                   at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
                   at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
                   at org.apache.catalina.core.StandardService.start(StandardService.java:516)
                   at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
                   at org.apache.catalina.startup.Catalina.start(Catalina.java:578)
                   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                   at java.lang.reflect.Method.invoke(Method.java:597)
                   at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
                   at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
              Caused by: java.lang.IllegalArgumentException: no such field: org.jboss.seam.ui.EntityConverter.session
                   at org.jboss.seam.util.Reflections.getField(Reflections.java:283)
                   at org.jboss.seam.Component.initInitializers(Component.java:497)
                   at org.jboss.seam.Component.<init>(Component.java:270)
                   at org.jboss.seam.Component.<init>(Component.java:221)
                   at org.jboss.seam.init.Initialization.addComponent(Initialization.java:1016)
                   ... 27 more


              This is the code:


                <component name="org.jboss.seam.ui.EntityConverter">
                  <property name="session">#{myDatabase}</property>
                </component>



              Am I doing something wrong?

              • 4. Re: s:convertEntity problem
                bashan

                Thanks,


                Can this line:


                <persistence:managed-persistence-context name="entityManager" auto-create="true"
                          persistence-unit-jndi-name="java:/rdmEntityManagerFactory" />



                work on Tomcat with Hibernate? I never managed to make it work...
                I use c3p0 with Hibernate, so I don't have any Datasource defined.
                Anyway, what are the benefits that I will gain from using SMPC over my current configuration in which I am using the Session directly and not the EntityManager?

                • 5. Re: s:convertEntity problem
                  gjeudy

                  Do you have the other parts in your config as well?


                  <persistence:hibernate-session-factory name="hibernateSessionFactory"/>
                  
                    <persistence:managed-hibernate-session name="myDatabase"
                                                           session-factory="#{hibernateSessionFactory}"
                                                           auto-create="true"/>
                  
                  



                  if so myDatabase should resolve to something...

                  • 6. Re: s:convertEntity problem
                    bashan

                    this is the relevant parts in my config:



                      <persistence:hibernate-session-factory name="hibernateSessionFactory"/>
                    
                      <persistence:managed-hibernate-session name="myDatabase"
                                                             session-factory="#{hibernateSessionFactory}"
                                                             auto-create="true"/>
                    
                      <transaction:hibernate-transaction session="#{myDatabase}"/>



                    By the way, it seems that the exception is complaining of simply not having session property:


                    Caused by: java.lang.IllegalArgumentException: no such field: org.jboss.seam.ui.EntityConverter.session




                    • 7. Re: s:convertEntity problem
                      mtorres

                      What version are you using? Im using 2.0.2.SP1. The method may not be there on older versions.

                      • 8. Re: s:convertEntity problem
                        bashan

                        Ok, it seems like the previous exception was fixed. but now, when I enter to the page I get this exception:


                        org.hibernate.TransientObjectException: The instance was not associated with this session
                             at org.hibernate.impl.SessionImpl.getIdentifier(SessionImpl.java:1375)
                             at org.hibernate.search.impl.FullTextSessionImpl.getIdentifier(FullTextSessionImpl.java:525)
                             at org.jboss.seam.persistence.HibernateSessionProxy.getIdentifier(HibernateSessionProxy.java:236)
                             at org.jboss.seam.framework.HibernateEntityIdentifier.<init>(HibernateEntityIdentifier.java:13)
                             at org.jboss.seam.ui.converter.entityConverter.HibernateEntityLoader.createIdentifier(HibernateEntityLoader.java:39)
                             at org.jboss.seam.ui.converter.entityConverter.AbstractEntityLoader.put(AbstractEntityLoader.java:50)
                             at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                             at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                             at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                             at java.lang.reflect.Method.invoke(Method.java:597)
                             at org.jboss.seam.util.Reflections.invoke(Reflections.java:21)
                             at org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:31)
                             at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56)
                             at org.jboss.seam.transaction.RollbackInterceptor.aroundInvoke(RollbackInterceptor.java:31)
                             at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
                             at org.jboss.seam.transaction.TransactionInterceptor$1.work(TransactionInterceptor.java:38)
                             at org.jboss.seam.util.Work.workInTransaction(Work.java:41)
                             at org.jboss.seam.transaction.TransactionInterceptor.aroundInvoke(TransactionInterceptor.java:32)
                             at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
                             at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:42)
                             at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
                             at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107)
                             at org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java:166)
                             at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:102)
                             at org.jboss.seam.ui.converter.entityConverter.HibernateEntityLoader_$$_javassist_6.put(HibernateEntityLoader_$$_javassist_6.java)
                             at org.jboss.seam.ui.converter.EntityConverter.getAsString(EntityConverter.java:77)
                             at org.jboss.seam.ui.converter.PrioritizableConverter.getAsString(PrioritizableConverter.java:67)
                             at org.jboss.seam.ui.converter.ConverterChain.getAsString(ConverterChain.java:123)
                             at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getFormattedValue(HtmlBasicRenderer.java:469)
                             at com.sun.faces.renderkit.html_basic.MenuRenderer.renderOption(MenuRenderer.java:502)
                             at com.sun.faces.renderkit.html_basic.MenuRenderer.renderOptions(MenuRenderer.java:757)
                             at com.sun.faces.renderkit.html_basic.MenuRenderer.renderSelect(MenuRenderer.java:811)
                             at com.sun.faces.renderkit.html_basic.MenuRenderer.encodeEnd(MenuRenderer.java:335)
                             at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:836)
                             at org.jboss.seam.ui.util.cdk.RendererBase.renderChild(RendererBase.java:190)
                             at org.jboss.seam.ui.util.cdk.RendererBase.renderChildren(RendererBase.java:166)
                             at org.jboss.seam.ui.renderkit.ValidateAllRendererBase.doEncodeChildren(ValidateAllRendererBase.java:33)
                             at org.jboss.seam.ui.util.cdk.RendererBase.encodeChildren(RendererBase.java:92)
                             at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:812)
                             at javax.faces.component.UIComponent.encodeAll(UIComponent.java:886)
                             at javax.faces.render.Renderer.encodeChildren(Renderer.java:137)
                             at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:812)
                             at javax.faces.component.UIComponent.encodeAll(UIComponent.java:886)
                             at javax.faces.component.UIComponent.encodeAll(UIComponent.java:892)
                             at com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:592)
                             at org.ajax4jsf.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:108)
                             at org.ajax4jsf.application.AjaxViewHandler.renderView(AjaxViewHandler.java:216)
                             at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:106)
                             at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
                             at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:144)
                             at javax.faces.webapp.FacesServlet.service(FacesServlet.java:245)
                             at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
                             at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                             at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:147)
                             at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                             at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                             at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
                             at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:85)
                             at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                             at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)
                             at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                             at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
                             at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                             at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:141)
                             at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:281)
                             at org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:56)
                             at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                             at org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:58)
                             at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                             at org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)
                             at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                             at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                             at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
                             at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
                             at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
                             at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
                             at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
                             at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
                             at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
                             at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
                             at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
                             at java.lang.Thread.run(Thread.java:619)



                        when I remove the EntityConverter the screen loads ok.


                        Am I missing something?

                        • 9. Re: s:convertEntity problem
                          gjeudy

                          ok now you fall in more common hibernate related exceptions. This exception means one of your entity is transient (not in the persistence context).


                          You need to make sure the entities are attached to the persistence context.


                          Can you give more details on how you populate your entity list that you pass to the entityConverter ?

                          • 10. Re: s:convertEntity problem
                            bashan

                            Thanks,


                            This is the code on my bean named upload (In the DAO there is a simple HQL):


                              public List getCategories()
                              {
                                return categoryDAO.getCategories();
                              }



                            And this is the code on my page:


                            <h:selectOneMenu id="category" value="#{video.category}" required="true">
                                          <s:selectItems value="#{upload.categories}"
                                                         var="category"
                                                         label="#{category.name}"
                                                         itemValue="#{category.categoryId}"
                                                         noSelectionLabel="-- Select Category --"/>
                                           <s:convertEntity />
                                        </h:selectOneMenu>



                            • 11. Re: s:convertEntity problem
                              gjeudy

                              Can you give more details about the dao (bean type, scope, etc..) and how you inject the Session you are using to get the categories ?

                              • 12. Re: s:convertEntity problem
                                bashan

                                This is the entire bean (I took off some irrelevant code that took space...):



                                @Name("upload")
                                @Scope(ScopeType.CONVERSATION)
                                public class UploadAction
                                {
                                  private static final int BUFFER_SIZE = 4096;
                                
                                  @In
                                  private FacesMessages facesMessages;
                                
                                  @In
                                  Session myDatabase;
                                
                                  @In
                                  CategoryDAO categoryDAO;
                                
                                  @Logger
                                  private Log log;
                                
                                  @In
                                  User user;
                                
                                  @In(create = true)
                                  private Video video;
                                
                                  private UploadedFile uploadedFile;
                                
                                  @End
                                  public String upload()
                                  {
                                    upload process...
                                  }
                                
                                  public UploadedFile getUploadedFile()
                                  {
                                    return uploadedFile;
                                  }
                                
                                  public void setUploadedFile(UploadedFile uploadedFile)
                                  {
                                    this.uploadedFile = uploadedFile;
                                  }
                                
                                  public List getCategories()
                                  {
                                    return categoryDAO.getCategories();
                                  }
                                }



                                This is the DAO:


                                @Name("categoryDAO")
                                @AutoCreate
                                public class CategoryDAO
                                {
                                  @Logger
                                  static Log log;
                                
                                  @In
                                  Session myDatabase;
                                
                                  public List<Category> getCategories()
                                  {
                                    return myDatabase.createQuery("select c from Category c where c.isActive = true").list();
                                  }
                                }
                                



                                This is my components.xml:


                                <components xmlns="http://jboss.com/products/seam/components"
                                            xmlns:core="http://jboss.com/products/seam/core"
                                            xmlns:persistence="http://jboss.com/products/seam/persistence"
                                            xmlns:security="http://jboss.com/products/seam/security"
                                            xmlns:transaction="http://jboss.com/products/seam/transaction"
                                            xmlns:mail="http://jboss.com/products/seam/mail"
                                            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                                            xsi:schemaLocation=
                                                "http://jboss.com/products/seam/core http://jboss.com/products/seam/core-2.0.xsd
                                                 http://jboss.com/products/seam/persistence http://jboss.com/products/seam/persistence-2.0.xsd
                                                 http://jboss.com/products/seam/security http://jboss.com/products/seam/security-2.0.xsd
                                                 http://jboss.com/products/seam/transaction http://jboss.com/products/seam/transaction-2.0.xsd
                                                 http://jboss.com/products/seam/components http://jboss.com/products/seam/components-2.0.xsd
                                                 http://jboss.com/products/seam/mail http://jboss.com/products/seam/mail-2.0.xsd">
                                
                                  <core:manager conversation-timeout="120000"
                                                concurrent-request-timeout="500"
                                                conversation-id-parameter="cid"/>
                                
                                  <persistence:hibernate-session-factory name="hibernateSessionFactory"/>
                                
                                  <persistence:managed-hibernate-session name="myDatabase"
                                                                         session-factory="#{hibernateSessionFactory}"
                                                                         auto-create="true"/>
                                
                                  <transaction:hibernate-transaction session="#{myDatabase}"/>
                                  
                                  <security:identity authenticate-method="#{authenticator.authenticate}"/>
                                
                                   <event type="org.jboss.seam.security.notLoggedIn">
                                       <action execute="#{redirect.captureCurrentView}"/>
                                   </event>
                                   <event type="org.jboss.seam.security.loginSuccessful">
                                       <action execute="#{redirect.returnToCapturedView}"/>
                                   </event>
                                
                                  <component name="org.jboss.seam.ui.EntityConverter">
                                    <property name="session">#{myDatabase}</property>
                                  </component>
                                
                                </components>
                                


                                Let me know if anything more is needed...


                                Thanks.

                                • 13. Re: s:convertEntity problem
                                  mtorres

                                  Try to get rid of itemValue from selectItems, that worked for me.

                                  • 14. Re: s:convertEntity problem
                                    gjeudy

                                    I see nothing wrong with this code. If I were you I would track the conversation by displaying the seam debug page at various intervals and verify that your DAO participates in the same conversation and basically nothing funky happens with the conversation and scoped variables.


                                    The other thing you can do is run in debug mode and see if the entity converter is using the same persistence context than the one used by the DAO (to load the categories).


                                    Excerpt from SessionImpl source code:


                                    public Serializable getIdentifier(Object object) throws HibernateException {
                                              errorIfClosed();
                                              checkTransactionSynchStatus();
                                              if ( object instanceof HibernateProxy ) {
                                                   LazyInitializer li = ( (HibernateProxy) object ).getHibernateLazyInitializer();
                                                   if ( li.getSession() != this ) {
                                                        throw new TransientObjectException( "The proxy was not associated with this session" );
                                                   }
                                                   return li.getIdentifier();
                                              }
                                              else {
                                                   EntityEntry entry = persistenceContext.getEntry(object);
                                                   if ( entry == null ) {
                                    This is where your exception gets thrown -->               throw new TransientObjectException( "The instance was not associated with this session" );
                                                   }
                                                   return entry.getId();
                                              }
                                         }
                                    



                                    Hope that helps.

                                    1 2 Previous Next