This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Hibernate Lazy Loading problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate Lazy Loading problem" Watch "Hibernate Lazy Loading problem" New topic
Author

Hibernate Lazy Loading problem

Parag Doshi
Ranch Hand

Joined: Jun 29, 2004
Posts: 317
I am laoding a complex domain object using hibernate at startup and storing it in a secondary cache. To load the object, i open a session, retrieve some data and close the session. As the object is complex in nature, there are certain associated objects which are lazily fetched.
These objects might be accessed at a later stage under specific business conditions. My problem is that when they are accessed, hibernate throws an error telling that the session is not open.
I understand now that the session has to be kept open to lazy fetch parts of an object. I can't leave the session open long enough as I dont know when the lazy objects would be required.

Is there a way to lazy fetch these associated objects without keeping the initial Session open?

Any assistance would be appreciated.

Thanks in advance,
Parag
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You will need to reassociate your object with an open Session. In your case its probably best done by opening a new Session and calling lock(POJO, LockMode) which reassociates an object with a session without the object to refresh its state. Then you can load the associations.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Parag Doshi
Ranch Hand

Joined: Jun 29, 2004
Posts: 317
Originally posted by Paul Sturrock:
You will need to reassociate your object with an open Session. In your case its probably best done by opening a new Session and calling lock(POJO, LockMode) which reassociates an object with a session without the object to refresh its state. Then you can load the associations.


Thanks for your reply Paul. I do not want to refresh the entire state of the object. I just want to load the associations. If I re-associate the object with a new Session, wouldn't it load the entire object again? My object is already cached, I just need the associations on-demand.

Parag
 
 
subject: Hibernate Lazy Loading problem