Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate Lazy Loading problem

 
Parag Doshi
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Parag Doshi
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic