java延迟加载,Spring如何允许延迟加载?

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值