用Spring HibernateDaoSupport延迟加载? [英] Lazy-loading with Spring HibernateDaoSupport?
问题描述
我的问题是HibernateDaoSupport如何处理延迟加载,因为在调用DAO之后,Session被关闭。
请看下面的伪代码:
DAO就像:
CommonDao扩展了HibernateDaoSupport {
Family getFamilyById(String id);
SubFamily getSubFamily(String familyid,String subfamilyid);
}
域模型如下:
家庭{
私人列表< SubFamily> subfamiles;
公开列表< SubFamily> getSubFamiles();
}
子家庭{
私人家庭家庭;
public Family getFamily();
$ b在应用程序中,我从app-context获取DAO,并希望执行以下操作。这可能与延迟加载有关,因为在每个方法(getFamilyById(),getSubFamily())会话之后的AFAIK被关闭。
CommonDAO dao = //从应用程序上下文获取bean;
Family famA = dao.getFamilyById(familyid);
//
//做一些东西
List< SubFamily> childrenA = fam.getSubFamiles();
SubFamily asubfamily = dao.getSubFamily(id,subfamilyid);
//
//做一些其他的东西
Family famB = asubfamily.getFamily();
解决方案
HibernateDaoSupport处理延迟加载,因为在调用DAO之后,Session被关闭。
DAO不会创建/关闭一个会话为每个电话,他们不负责这一点,这通常使用在视图中打开会话模式(Spring为此提供了一个过滤器或拦截器)。但这是针对网络应用的。
在一个swing应用程序中,一个解决方案是使用长期会议。您必须决定明确定义的点来关闭会话以释放内存。对于小型应用程序,这通常很简单并且可以工作。对于更大的(即现实生活应用程序),正确的解决方案是每帧使用一个会话/内部框架/对话框。它很难管理,但会扩展。
您可能想要阅读的一些主题:
li>
保持休眠会话在swing客户端打开(这一个是恕我直言,最有趣的,特别是#9) Hibernate和Swing演示应用程序
Greetings I am developing a non-webapplication using Spring+Hibernate.
My question is how the HibernateDaoSupport handles lazy-loading , because after a call do DAO , the Session is closed.
Take a look at following psuedo-code:
DAO is like:
CommonDao extends HibernateDaoSupport{
Family getFamilyById(String id);
SubFamily getSubFamily(String familyid,String subfamilyid);
}
Domain model is like:
Family{
private List<SubFamily> subfamiles;
public List<SubFamily> getSubFamiles();
}
SubFamily{
private Family family;
public Family getFamily();
}
In the application I get DAO from app-context and want to following operations.Is this possible to do with lazy-loading because AFAIK after every method (getFamilyById() , getSubFamily() ) the session is closed.
CommonDAO dao=//get bean from application context;
Family famA=dao.getFamilyById(familyid);
//
//Do some stuff
List<SubFamily> childrenA=fam.getSubFamiles();
SubFamily asubfamily=dao.getSubFamily(id,subfamilyid);
//
//Do some other stuff
Family famB=asubfamily.getFamily();
解决方案
My question is how the HibernateDaoSupport handles lazy-loading , because after a call to DAO, the Session is closed.
The DAOs don't create/close a Session for each call, they are not responsible for this and this is usually done using the "Open Session in View" pattern (Spring provide a filter or an interceptor for this). But this is for web apps.
In a swing app, one solution is to use long session. You'll have to decide well-defined points at which to close the session to release memory. For small applications, this is usually straightforward and will work. For bigger (i.e. real life apps), the right solution is to use one session per frame/internal frame/dialog. It's harder to manage but will scale.
Some threads you might want to read:
- hibernate LazyInitializationException in rich client app
- Spring/Hibernate long session support or best practice?
- Keep hibernate session open in swing client? (this one is IMHO the most interesting, especially #9)
- And the Hibernate and Swing demo app
这篇关于用Spring HibernateDaoSupport延迟加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!