Spring/Hibernate/Richfaces应用程序中的延迟加载问题

Spring/Hibernate/Richfaces应用程序中的延迟加载问题,hibernate,spring,jsf,richfaces,lazy-loading,Hibernate,Spring,Jsf,Richfaces,Lazy Loading,在一个Spring应用程序中,我使用了带有Hibernate的component-rich:tree,它使用延迟加载显示应用程序的层次结构。 问题是Hibernate会话已关闭,这会阻止延迟加载,并且在呈现页面时我会收到异常。 最后,我添加了opensessionViewFilter,它允许我在呈现页面之前保持会话 <filter> <filter-name>openSessionInViewFilter</filter-name>

在一个Spring应用程序中,我使用了带有Hibernate的component-rich:tree,它使用延迟加载显示应用程序的层次结构。 问题是Hibernate会话已关闭,这会阻止延迟加载,并且在呈现页面时我会收到异常。 最后,我添加了opensessionViewFilter,它允许我在呈现页面之前保持会话

<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>
请帮我查看ajax组件的延迟加载列表

这是我的xhtml页面:

<h:form>
            <rich:panel>
                <rich:tree>
                    <rich:recursiveTreeNodesAdaptor roots="#{treeBean.srcRouts}"
                        var="item" nodes="#{item.nodes}">
                    </rich:recursiveTreeNodesAdaptor>
                </rich:tree>
            </rich:panel>

</h:form>

下面是Bean:TreeBean.java

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;
}
}
公共类树bean{
私有列表路由;
@自动连线
私人申请道申请道;
@交易的
公共列表getSrcRouts(){
List ListNode=new ArrayList();
if(srcRouts==null)
{
List=applicationDAO.findAll();
用于(应用程序:列表)
{
TreeNode TreeNode=新的TreeNode();
treeNode.setApplication(app);
添加(treeNode);
}
srcRouts=ListNode;
}
返回路由;
}
公共无效设置路由(列表路由){
this.srcRouts=srcRouts;
}
}
下面是Bean TreeNode.java:

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import myPackage.entities.Application;
import myPackage.entities.Module;
import myPackage.entities.Page;
import myPackage.services.ApplicationDAO;

public class TreeNode {
    private List<TreeNode> nodes;
    private ApplicationDAO applicationDAO;
    private String libelle;
    private Application application;
    private Module module;
    private Page page;

    public Application getApplication() {
        return application;
    }

    public void setApplication(Application application) {
        this.application = application;
    }

    public Module getModule() {
        return module;
    }

    public void setModule(Module module) {
        this.module = module;
    }

    public Page getPage() {
        return page;
    }

    public void setPage(Page page) {
        this.page = page;
    }

    public String getLibelle() {
        return libelle;
    }

    public void setLibelle(String libelle) {
        this.libelle = libelle;
    }

    public ApplicationDAO getApplicationDAO() {
        return applicationDAO;
    }

    public void setApplicationDAO(ApplicationDAO applicationDAO) {
        this.applicationDAO = applicationDAO;
    }

    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;
    }

    public void setNodes(List<TreeNode> nodes) {
        this.nodes = nodes;
    }

    public String toString() {
        String libelle="";

        // noeud application
        if (application != null) {
            libelle = application.getNomApplication();
        }
        // noeud module
        else if (module != null) {
            libelle = module.getNomModule();
        }
        // noeud page
        else if (page != null) {
            libelle = page.getNomPage();
        }
        return libelle;
    }
}
import java.util.ArrayList;
导入java.util.List;
导入java.util.Set;
导入myPackage.entities.Application;
导入myPackage.entities.Module;
导入myPackage.entities.Page;
导入myPackage.services.ApplicationDAO;
公共级树节点{
私有列表节点;
私人申请道申请道;
私串诽谤;
私人申请;
专用模块;
私人网页;
公共应用程序getApplication(){
退货申请;
}
公共应用程序(应用程序){
这个应用程序=应用程序;
}
公共模块getModule(){
返回模块;
}
公共无效设置模块(模块){
this.module=模块;
}
公共页面getPage(){
返回页面;
}
公共无效设置页(第页){
this.page=page;
}
公共字符串getLibelle(){
报复诽谤;
}
公共无效诽谤(字符串诽谤){
this.libelle=诽谤;
}
公共应用程序dao getApplicationDAO(){
返回申请DAO;
}
公共无效设置ApplicationDAO(ApplicationDAO ApplicationDAO){
this.applicationDAO=applicationDAO;
}
公共列表getNodes(){
List listneoud=new ArrayList();
设置设置模块;
设置设置页面;
如果(节点==null){
//noeud应用程序
if(应用程序!=null){
setModule=this.application.getModules();
用于(模块:设置模块){
if(module.getModule()==null){
TreeNode TreeNode=新的TreeNode();
treeNode.module=模块;
添加(treeNode);
}
}
}
//noeud模
else if(模块!=null){
setModule=this.module.getModules();
用于(模块:设置模块){
TreeNode TreeNode=新的TreeNode();
treeNode.module=模块;
添加(treeNode);
}
setPage=this.module.getPages();
用于(第页:设置页){
TreeNode TreeNode=新的TreeNode();
treeNode.page=页面;
添加(treeNode);
}
}
节点=listneoud;
}
返回节点;
}
公共void集合节点(列表节点){
this.nodes=节点;
}
公共字符串toString(){
字符串诽谤=”;
//noeud应用程序
if(应用程序!=null){
libelle=application.getnoapplication();
}
//noeud模
else if(模块!=null){
libelle=module.getNomModule();
}
//诺厄德页
else if(第页!=null){
libelle=page.getNomPage();
}
报复诽谤;
}
}
我使用组件树来显示用户添加的要测试的应用程序……实际上,我尝试创建一个运行用户选择的测试的应用程序。 要指定,我的节点是应用程序、模块或页面

这是我的通用DAO:

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@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;
    }

}
import java.io.Serializable;
导入java.util.List;
导入org.hibernate.Query;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.orm.hibernate3.HibernateTemplate;
导入org.springframework.transaction.annotation.Propagation;
导入org.springframework.transaction.annotation.Transactional;
@交易的
公共类GenericDAOHibernateImpl实现
通用刀{
私人阶级类型;
@自动连线
私有HibernateTemplate HibernateTemplate;
公共HibernateTemplate getHibernateTemplate(){
返回hibernateTemplate;
}
公共void setHibernateTemplate(HibernateTemplate HibernateTemplate){
this.hibernateTemplate=hibernateTemplate;
}
公共GenericDAOHibernateImpl(类类型){
this.type=type;
}
公共PK创建(TO){
返回(PK)getHibernateTemplate().save(o);
}
公共T读(主键id){
return(T)getHibernateTemplate().get(type,id);
}
公开作废更新(TO){
getHibernateTemplate().update(o);
}
公共作废删除(TO){
getHibernateTemplate().delete(o);
}
@抑制警告(“未选中”)
公共列表findAll(){
List-toExport=null;
toExport=(列表)getHibernateTemplate().find(
“from”+type.getName());
返回出口;
}
@抑制警告(“未选中”)
公共T findByID(主键id){
import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@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;
    }

}