views:

486

answers:

4

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?

A: 

If this was a web application I would use the OpenSessionInViewFilter

Vinodh Ramasubramanian
I am not sure how this is related to the question.
BalusC
OpenSessionInViewFilter allows the session to stay open through out the request preventing exceptions when lazy load collections are accessed in the view. If such a filter is not used in Spring, the session is closed and will result in exception even with in the same request. I just drew a parallel between this and the OPs question "how come the session stays in scope and allows you to lazy-load a collection?"
Vinodh Ramasubramanian
+1  A: 

Hibernate returns a custom implementation of the collection which does the loading only whenever any of the collection methods is been invoked. This collection has been constructed with the session as argument.

Do a sysout of getSomeCollection().getClass() to see which Hibernate custom class it is and check the appropriate javadocs/sourcecode to see how exactly they did it.

If you ever questioned the sense/use/value of interfaces and declaring against interfaces, now, this is a good example. You didn't see anything from it, did you? ;)

BalusC
but the question is, the session has to remain active right?
mrblah
It is. You need to access the collection inside the **same** session. You have control over Hibernate session lifetime yourself. You can have for example a Hibernate session per HTTP request or a Hibernate session per HTTP session.
BalusC
BalusC, i see, so where do I set the session lifetime?
mrblah
if you're using a HibernateDAOSupport object as your base class or using HibernateTemplate then you're managing the session lifetime directly. It stays open until you close it.
Jherico
+1  A: 

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

It's typically initialized on a per-request basis using ... (wait for it)

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

It: org.springframework.orm.hibernate3.HibernateTemplate

... is the heart of Spring's integration with Hibernate. HibernateTemplate will initialize the session as necessary (or pull an already existing one from a ThreadLocal store) and provide it to any callbacks you give to the HibernateTemplate#execute* methods.

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?

Also done by HibernateTemplate, and ...

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

Done by HibernateTransactionManager, if you have one configured in your applicationContext.

Alex Marshall
+5  A: 

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.

Pascal Thivent
+1 for using the debugger
Alex Marshall