Kaustubh G Sharma wrote:I am still confused
Suppose you're in a managed session. You instantiate a Parent object. The Parent object has a collection of Child objects.
If you map the relationship so that
lazy is true, then Hibernate will not fully construct all the Child objects and place them in the Parent's collection. Instead, Hibernate will provide a collection of proxy objects, one for each Child, where each proxy object knows the id of its corresponding Child.
If you use the Parent object but never reference its collection of Child objects, then the Parent merely retains its little bag of id-bearing proxies. However, if you try to use the Parent's collection, Hibernate will then (as needed) "hydrate" the proxy objects with the rest of the state and functionality of the Child objects they represent.
On the other hand, if
lazy is false, then Hibernate will skip this efficiency and fully realize every Child in a Parent's collection whenever that Parent is instantiated.
The lazy way is lighter in weight and more performant than fully realizing a collection of Child objects every time a Parent is instantiated.
Note also that this lazy behavior is
session dependent. If you save a reference to some Parent and then try to use that Parent's collection from within a different session with which the Parent has not been properly associated (e.g., via merging or loading), then hydrating the Child objects from their proxies will be impossible, and you'll receive that most famous of Hibernate complaints, the LazyInitializationException.