Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sample code for lazy fetch in hibernate

 
Rajendra Prakash
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lazy fetch : lazy=true means not to load child objects while loading parent objects. can you example code for lazy fetching.
 
sumit anand kumar
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah thats correct you are not loading the child object entirely. Either you are loading a proxy object for your child object and at run time its replaced by the original object or just you are loading your child object's reference ID in your parent object
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing to do with Servlets. Moving...
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no sample code to post--set lazy=true, get an object that has children. The children won't be fetched until you try to access them.
 
Ran Pleasant
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajendra Prakash wrote:Lazy fetch : lazy=true means not to load child objects while loading parent objects. can you example code for lazy fetching.


One important thing to note is that you cannot use "lazy=true" if you plan to send your entity objects outside of the context of the Hibernate Session object because your entity object keeps a reference to the session. For example, if you load the parent entity in a Session EJB and pass the parent entity to a servlet and then call the getChildren() method that is marked as "lazy=true" an exception will be throw due to an invalid session. The exception will be thrown even if you make a call to the method before passing the parent entity to the servlet. Therefore, with some well thought out exceptions, if you are working within an EJB enviornment it is best to define all parent-child relations as "lazy=false".

Ran
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic