Announcement Announcement Module
Collapse
No announcement yet.
unable to lazy load object within richfaces component Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • unable to lazy load object within richfaces component

    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.

    Code:
        <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.
    Code:
        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)
    here is my xhtml page:
    Code:
    <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)

    Code:
    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:

    Code:
    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:

    Code:
    <?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:

    Code:
    @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;
        	}
        
        }
    Environements:Richfaces 3.3.2 SR1,Hibernate 3,Jsf 1.2, Spring 2.5.5
    Please can you help me to lazy load list within Richfaces component.
    thank you in advance for your help.
Working...
X