Jan 9th, 2006, 10:33 AM
Saving persistent (lazy) objects in http session
I am using Spring + Hibernate + Tapestry (as part of Appfuse 1.8.2). I am using Spring's OpenSessionInView filter for Hibernate (defined in web.xml).
I loaded an object from the database using hibernate (lazy = true), and saved that object in my http session. In future http requests, I manipulate that object (In Tapestry, that objest is saved in the Visit class). However, since the orignal hibernate session gets closed at the end of the first http request cycle, subsequent access to that object, which would trigger any database activity, fails with a message like: "Could not initialize proxy - the owning Session was closed".
I need to save this object in my http session for future reference. However, this object has reference to other objects which are lazily loaded. What is a recommended method of handling this scenario? Basically, I need a mechanism where I would attach (lock, update) the current hibernate session to an object, which was already loaded by a previous hibernate session. I would like to do this in a transparent way.
Additional info about my problem can be found here:
Jan 10th, 2006, 01:28 AM
I would use a SessionReattachFilter that does the following:
1. reattaches some objects from the HTTP Session to the Hibernate Session before continuing the request chain
2. deattaches some objects from HTTP Session to the Hibernate Session after the request chain is processed.
You could configure this through a set of [HTTP Session attribute name -- LockMode] pairs.
The OpenSessionInView filter should be before this one in the filter chain.
Alternatively, you could use the Hibernate session per HTTP session strategy, with connect/disconect of the Hibernate session on each request. But this would most likely break the functionality of the existing code.
I think this issues has come up on some other threads.