Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Sample code for lazy fetch in hibernate

 
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nothing to do with Servlets. Moving...
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Paper beats rock. Scissors beats tiny ad.
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic