使用Spring HibernateDaoSupport进行延迟加载?(Lazy

编程入门 行业动态 更新时间:2024-10-20 20:47:59
使用Spring HibernateDaoSupport进行延迟加载?(Lazy-loading with Spring HibernateDaoSupport?)

问候我正在使用Spring + Hibernate开发一个非Web应用程序。 我的问题是HibernateDaoSupport如何处理延迟加载,因为在调用DAO之后,Session被关闭了。

看看下面的伪代码:

DAO就像:

CommonDao extends HibernateDaoSupport{ Family getFamilyById(String id); SubFamily getSubFamily(String familyid,String subfamilyid); }

域模型如下:

Family{ private List<SubFamily> subfamiles; public List<SubFamily> getSubFamiles(); } SubFamily{ private Family family; public Family getFamily(); }

在应用程序中,我从app-context获取DAO并希望执行以下操作。这可能与延迟加载有关,因为AFAIK在每个方法(getFamilyById(),getSubFamily())之后关闭会话。

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();

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();

最满意答案

我的问题是HibernateDaoSupport如何处理延迟加载,因为在调用DAO之后,Session被关闭了。

DAO不会为每个调用创建/关闭一个Session,它们不对此负责,这通常使用“ Open View in View ”模式(Spring为此提供过滤器或拦截器)来完成。 但这适用于网络应用。

在swing应用程序中,一种解决方案是使用长会话 。 您必须决定关闭会话以释放内存的明确定义的点。 对于小型应用程序,这通常很简单并且可以工作。 对于更大的(即真实应用程序),正确的解决方案是每帧/内部框架/对话使用一个会话。 管理起来比较难,但会扩大规模。

您可能想要阅读的一些主题:

富客户端应用程序中的hibernate LazyInitializationException Spring / Hibernate长会话支持还是最佳实践? 在swing客户端中保持hibernate会话打开? (这是恕我直言最有趣的,特别是#9) 以及Hibernate和Swing演示应用程序

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

更多推荐

本文发布于:2023-07-06 07:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047365.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:加载   Spring   HibernateDaoSupport   Lazy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!