1 Reply Latest reply on Aug 31, 2010 9:08 AM by Stan Silvert

    unable to lazy load object within richfaces component

    Ahmed Zaroui Newbie
      Hello,
      Im developping, a spring application that display application's heirarchy within a richFaces component called <rich:tree>,because of the amount of data we have to lazy load it... 
      The problem is that the Hibernate session is closed before tha page is rendered  which prevent lazy loading and i receive org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role, when the page is rendered.
      I added opensessionViewFilter that's allows me to keep the session until the page is rendered.
          <filter>
                    <filter-name>openSessionInViewFilter</filter-name>
                    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
                    <init-param>
                         <param-name>singleSession</param-name>
                         <param-value>true</param-value>
                    </init-param>
                    <init-param>
                         <param-name>sessionFactoryBeanName</param-name>
                         <param-value>sessionFactory</param-value>
                    </init-param>
               </filter>
          
               <filter-mapping>
                    <filter-name>openSessionInViewFilter</filter-name>
                    <url-pattern>/*</url-pattern>
               </filter-mapping>
      
      this solution partially solved the problem: the page is rendered without problems and the <rich:Tree>  component is displayed but when ajax calls are made (navigation between nodes) I got the same error of lazy loading exception because the Hibernate session is closed when richfaces component try to display data.
          org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: myPackage.entities.Module.modules, no session or session was closed
           at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
           at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
           at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343)
           at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
           at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:163)
           at com.cylande.utilities.TreeNode.getNodes(TreeNode.java:77)
           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 javax.el.BeanELResolver.getValue(BeanELResolver.java:62)
           at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:54)
           at com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:72)
           at org.apache.el.parser.AstValue.getValue(AstValue.java:118)
           at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
           at com.sun.facelets.el.TagValueExpression.getValue(TagValueExpression.java:71)
           at org.richfaces.component.html.HtmlRecursiveTreeNodesAdaptor.getNodes(HtmlRecursiveTreeNodesAdaptor.java:160)
           at org.richfaces.component.UIRecursiveTreeNodesAdaptor$1.getData(UIRecursiveTreeNodesAdaptor.java:74)
           at org.richfaces.model.StackingTreeModel.getDataModel(StackingTreeModel.java:80)
           at org.richfaces.model.StackingTreeModel.isEmpty(StackingTreeModel.java:107)
           at org.richfaces.model.StackingTreeModel.isLeaf(StackingTreeModel.java:264)
           at org.richfaces.component.UITree.isLeaf(UITree.java:534)
           at org.richfaces.renderkit.html.TreeNodeRenderer.doEncodeBegin(TreeNodeRenderer.java:105)
           at org.richfaces.renderkit.html.TreeNodeRenderer.doEncodeBegin(TreeNodeRenderer.java:86)
           at org.ajax4jsf.renderkit.RendererBase.encodeBegin(RendererBase.java:100)
           at javax.faces.component.UIComponentBase.encodeBegin(UIComponentBase.java:813)
           at org.ajax4jsf.renderkit.RendererBase.renderChild(RendererBase.java:275)
           at org.richfaces.renderkit.TreeRendererBase$DataVisitorWithLastElement.process(TreeRendererBase.java:255)
           at org.richfaces.model.AbstractTreeDataModel.processElement(AbstractTreeDataModel.java:111)
           at org.richfaces.model.StackingTreeModel.doWalk(StackingTreeModel.java:294)
           at org.richfaces.model.StackingTreeModel$Visitor1.process(StackingTreeModel.java:416)
           at org.richfaces.model.StackingTreeModel$ShiftingDataVisitor.end(StackingTreeModel.java:461)
           at org.richfaces.model.StackingTreeModel.doWalk(StackingTreeModel.java:348)
           at org.richfaces.model.StackingTreeModel.walk(StackingTreeModel.java:383)
           at org.richfaces.component.UITree.walk(UITree.java:422)
           at org.richfaces.renderkit.TreeRendererBase.writeContent(TreeRendererBase.java:683)
           at org.richfaces.renderkit.TreeRendererBase.encodeAjaxChildren(TreeRendererBase.java:475)
           at org.ajax4jsf.component.UIDataAdaptor.encodeAjaxChild(UIDataAdaptor.java:454)
           at org.ajax4jsf.renderkit.AjaxChildrenRenderer.encodeAjaxComponent(AjaxChildrenRenderer.java:146)
           at org.ajax4jsf.renderkit.AjaxChildrenRenderer.encodeAjaxChildren(AjaxChildrenRenderer.java:83)
           at org.ajax4jsf.renderkit.AjaxChildrenRenderer.encodeAjaxComponent(AjaxChildrenRenderer.java:157)
           at org.ajax4jsf.renderkit.AjaxChildrenRenderer.encodeAjaxChildren(AjaxChildrenRenderer.java:83)
           at org.ajax4jsf.renderkit.AjaxChildrenRenderer.encodeAjaxComponent(AjaxChildrenRenderer.java:157)
           at org.ajax4jsf.renderkit.AjaxContainerRenderer.encodeAjax(AjaxContainerRenderer.java:123)
           at org.ajax4jsf.component.AjaxViewRoot.encodeAjax(AjaxViewRoot.java:680)
           at org.ajax4jsf.component.AjaxViewRoot.encodeChildren(AjaxViewRoot.java:551)
           at javax.faces.component.UIComponent.encodeAll(UIComponent.java:930)
           at com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:592)
           at org.ajax4jsf.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:100)
           at org.ajax4jsf.application.AjaxViewHandler.renderView(AjaxViewHandler.java:176)
           at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:110)
           at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
           at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
           at javax.faces.webapp.FacesServlet.service(FacesServlet.java:266)
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
           at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:178)
           at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
           at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:388)
           at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:515)
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
           at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:70)
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
           at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
           at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
           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:191)
           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:293)
           at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
           at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
           at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
           at java.lang.Thread.run(Thread.java:619)
      
      here is my xhtml page:
          <h:form>
                         <rich:panel>
                              <rich:tree>
                                   <rich:recursiveTreeNodesAdaptor roots="#{treeBean.srcRouts}"
                                        var="item" nodes="#{item.nodes}">
                                   </rich:recursiveTreeNodesAdaptor>
                              </rich:tree>
                         </rich:panel>
          
          </h:form>
      
      Here is the Bean:TreeBean.java(that  bean is used by rich:tree to find nodes)
          public class TreeBean {
          private List<TreeNode> srcRouts;
          @Autowired
          private ApplicationDAO applicationDAO;
          @Transactional
          public List<TreeNode> getSrcRouts() {
               List<TreeNode> ListNode=new ArrayList<TreeNode>();
               if(srcRouts==null)
               {
                    List<Application> list=applicationDAO.findAll();
                    for(Application app:list)
                    {
                         TreeNode treeNode=new TreeNode();
                         treeNode.setApplication(app);
                         ListNode.add(treeNode);
                    }
                    srcRouts=ListNode;
               }
               return srcRouts;
          }
          
          public void setSrcRouts(List<TreeNode> srcRouts) {
               this.srcRouts = srcRouts;
          }
          }
      
      Here is the Bean TreeNode.java:
          public class TreeNode {
               private List<TreeNode> nodes;
               private ApplicationDAO applicationDAO;
               private String libelle;
               private Application application;
               private Module module;
               private Page page;
          
               //getters and setters
          
               public List<TreeNode> getNodes() {
                    List<TreeNode> listNoeud = new ArrayList<TreeNode>();
                    Set<Module> setModule;
                    Set<Page> setPage;
                    if (nodes == null) {
                         // noeud application
                         if (application != null) {
                              setModule = this.application.getModules();
                              for (Module module : setModule) {
                                   if(module.getModule()==null){
                                   TreeNode treeNode = new TreeNode();
                                   treeNode.module = module;
                                   listNoeud.add(treeNode);
                              }
                              }
                         }
                         // noeud module
                         else if (module != null) {
                              setModule = this.module.getModules();
                              for (Module module : setModule) {
                                   TreeNode treeNode = new TreeNode();
                                   treeNode.module = module;
                                   listNoeud.add(treeNode);
                              }
                              setPage = this.module.getPages();
                              for (Page page : setPage) {
                                   TreeNode treeNode = new TreeNode();
                                   treeNode.page = page;
                                   listNoeud.add(treeNode);
                              }
                         }
                         nodes=listNoeud;
                    }
          
                    return nodes;
               }
          }
      
      NB: I use the component tree to display the applications structure added by the user to be tested ...In fact try to create an application that's run tests chosen by the user.
      to specify, a node for me is an application, a module or a page
      Here is mapping file for Application entity:
      <?xml version="1.0"?>
      <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                                         "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
       <!-- Generated 10 ao?t 2010 11:19:43 by Hibernate Tools 3.3.0.GA -->
      <hibernate-mapping>
       <class name="myPackage.entities.Application" schema="AHMED"
       table="APPLICATION">
       <id name="idApplication" type="int">
       <column name="ID_APPLICATION" precision="6" scale="0" />
       <generator class="assigned" />
       </id>
       <property generated="never" lazy="false" name="nomApplication"
       type="string">
       <column length="30" name="NOM_APPLICATION" />
       </property>
       <property generated="never" lazy="false" name="descriptionApplication"
       type="clob">
       <column name="DESCRIPTION_APPLICATION" />
       </property>
       <property generated="never" lazy="false" name="dateLivraison"
       type="date">
       <column length="7" name="DATE_LIVRAISON" />
       </property>
       <set fetch="select" inverse="true" lazy="true" name="modules"
       sort="unsorted" table="MODULE">
       <key>
       <column name="ID_APPLICATION" not-null="true" precision="6"
       scale="0" />
       </key>
       <one-to-many class="myPackage.entities.Module" />
       </set>
       <set fetch="select" inverse="true" lazy="true" name="tests" sort="unsorted"
       table="TEST">
       <key>
      <column name="ID_APPLICATION" precision="6" scale="0" />
      </key>
      <one-to-many class="myPackage.entities.Test" />
      </set>
      </class>
      </hibernate-mapping>
      
      
      Here is my Generic DAO:
          @Transactional
          public class GenericDAOHibernateImpl<T, PK extends Serializable> implements
                    GenericDAO<T, PK> {
               private Class<T> type;
               @Autowired
               private HibernateTemplate hibernateTemplate;
          
               public HibernateTemplate getHibernateTemplate() {
                    return hibernateTemplate;
               }
          
               public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
                    this.hibernateTemplate = hibernateTemplate;
               }
          
               public GenericDAOHibernateImpl(Class<T> type) {
                    this.type = type;
               }
          
               public PK create(T o) {
                    return (PK) getHibernateTemplate().save(o);
               }
          
               public T read(PK id) {
                    return (T) getHibernateTemplate().get(type, id);
               }
          
               public void update(T o) {
                    getHibernateTemplate().update(o);
               }
          
               public void delete(T o) {
                    getHibernateTemplate().delete(o);
               }
          
               @SuppressWarnings("unchecked")
               public List<T> findAll() {
                    List<T> toExport = null;
                    toExport = (List<T>) getHibernateTemplate().find(
                              "from "+ type.getName());
                    return toExport;
               }
          
               @SuppressWarnings("unchecked")
               public T findByID(PK id) {
                    T entity = null;
                    entity =  (T)getHibernateTemplate().get(type, id);
                    return entity;
               }
          
          }
      

       

      Please can you help me to lazy load list within Richfaces component.

      thank you in advance for your help.

      Environements:Richfaces 3.3.2 SR1,Hibernate 3,Jsf 1.2, Spring 2.5.5