• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What are Lazy fetching and Early fetching?

 
Thirupathi Neela
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I am unable to understand the difference between Lazy fetching and Early fetching.
Could anyone give a small explanation what those are? and when to use which one?

regards
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both terms refer to data associated to the object you load.

Eager fetching: you use this if you know up front what data you need from the database. So if I have a Person who has an Adderss and I'm getting it for a screen that allows users to manage all their details (and details means everything about them) I might eagerly fetch both Person and the associated Address. In Hibernate HQL this would be soemthing like:


Lazy fetching: this is the normal behaviour for more ORMs. Here the associaed data is only loaded on request. Again in Hibernate this would be done by defining this in the mapping. Consider again the Person/Address example. If you changes the screen to allow people to manage their details and optionally change thier address you would lazily load the associated Address only if the user chose to manage it.
 
Thirupathi Neela
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Paul.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic