Spring如何允许延迟加载? [英] how does spring allow for lazy-loading?

查看:295
本文介绍了Spring如何允许延迟加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



return ..getHibernateTemplate(get by id); $ b $

如果您在Dao方法中使用(伪代码) b

现在说实体有一个延迟加载的集合。一旦你使用hibernateTemplate帮助器方法从你的Dao返回,会话如何保持在作用域中,并允许你延迟加载一个集合?

会话在每个请求的基础上进行全球级别的初始化和承诺?

更新



请解释确切地调用'getcurrentsession'的地方,以及它何时实际关闭/提交?



据我所知,spring框架必须处理会话生命周期,它在哪里执行此操作?在什么时候在请求生命周期?



它正在处理工作单元也在哪里/如何?

因为 Session 尚未关闭,因此您的实体仍然是

em> Persistent (与 Detached 对象状态相反)。只要您的实体未被分离,您可以延迟加载集合和代理。请参阅 10.1一章。 Hibernate对象状态以了解关于这些状态的更多细节(了解它们和使用的术语非常重要)。
$ b


会话在每个请求的基础上在全局级初始化并提交吗?


对于Web应用程序,通常是每个请求。正如 HibernateTemplate


延迟加载也可以用于打开Hibernate会话,无论是在事务中还是在OpenSessionInViewFilter / Interceptor中。

如果你看看 OpenSessionInViewFilter OpenSessionInViewInterceptor ,您会发现它们略有不同,但是它们都将Hibernate会话绑定到整个处理请求并提供Open Session in View模式的实现。


请详细解释 getcurrentsession'被创建,并且它什么时候被实际关闭/提交?

您可以查看源代码并使用一个调试器知道:)看看 HibernateTemplate ,更准确地说是 doExecute()方法,这是获取会话的地方。对于关闭/提交,看看前面提到的 OpenSessionInViewFilter / Interceptor ,都有用于此目的的方法。 p>


据我所知,Spring框架必须处理会话生命周期,它在哪里做到这一点?在请求生命周期的哪一点?


我想我已经介绍了这部分:会话是在请求开始时创建的,
$ b


它正在处理工作单元也在哪里/如何?
blockquote>

我不确定要得到这个。对我来说,Hibernate的Session 工作单元模式的一个实现。所以这个问题实际上与前一个问题是一样的。



PS:我提供了一些链接,表明一切都清楚地记录在案。 Spring和Hibernate有非常好的文档和javadoc。利用这一点,自己看看,亲自查看代码,使用调试器,您将学到更多。


If you have a call in a Dao method like (pseudo code):

return ..getHibernateTemplate( get by id );

Now say that entity has a lazy-loaded collection. Once you return from your Dao using the hibernateTemplate helper method, how come the session stays in scope and allows you to lazy-load a collection?

Is the session initialized and committed at a global level on a per request basis?

Update

Please explain where exactly the call to 'getcurrentsession' is made, and when is it actually closed/committed?

From what I understand, the spring framework has to handle the session lifecycle, where does it do this? at what point the in the requests lifecycle?

It is handling the Unit of work also, where/how?

解决方案

Once you return from your Dao using the hibernateTemplate helper method, how come the session stays in scope and allows you to lazy-load a collection?

Because the Session hasn't been closed yet and your entity is thus still Persistent (as opposed to the Detached object state). As long as your entity has not been detached, you can lazy load collections and proxies. See chapter 10.1. Hibernate object states for more details on these states (it's very important to understand them and the terminology used).

Is the session initialized and committed at a global level on a per request basis?

With web applications, it's typically per request. As mentioned in the javadoc of HibernateTemplate:

Lazy loading will also just work with an open Hibernate Session, either within a transaction or within OpenSessionInViewFilter/Interceptor.

And if you look at the javadoc of OpenSessionInViewFilter or OpenSessionInViewInterceptor, you'll read that they are slightly different but both binds a Hibernate Session to the thread for the entire processing of the request and provide an implementation of the "Open Session in View" pattern.

Please explain where exactly the call to 'getcurrentsession' is made, and when is it actually closed/committed?

You could look at the sources and use a debugger for this you know :) Look at HibernateTemplate, more precisely the doExecute() method, this is where the session is obtained. For the close/commit, look at the previously mentioned OpenSessionInViewFilter/Interceptor, both have methods for this purpose.

From what I understand, the spring framework has to handle the session lifecycle, where does it do this? at what point the in the requests lifecycle?

I think I covered that part: the session is created at the start of a request and closed at the end.

It is handling the Unit of work also, where/how?

I'm not sure to get this one. To me, Hibernate's Session is an implementation of the unit of work pattern. So this question is actually the same as the previous one.

PS: I provided some links that show that everything is actually clearly documented. Spring and Hibernate have extremely nice documentation and javadoc. Take advantage of that, look at them by yourself, look at the code by yourself, use your debugger, you'll learn a lot more.

这篇关于Spring如何允许延迟加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆