| Author |
Lazyness in hibernate?
|
Kaustubh G Sharma
Ranch Hand
Joined: May 13, 2010
Posts: 1145
|
|
|
I come to know about a problem regarding hibernate. That from DB your fetching 4 rows but getting only one value and someone told be this is regarding the laziness exception of the hibernate.. What is Laziness feature in hibernate. Tried google in it but not get my answer
|
No Kaustubh No Fun, Know Kaustubh Know Fun..
|
 |
Shanky Sohar
Ranch Hand
Joined: Mar 17, 2010
Posts: 1046
|
|
|
check this may help
|
SCJP6.0,My blog Ranchers from Delhi
|
 |
Kaustubh G Sharma
Ranch Hand
Joined: May 13, 2010
Posts: 1145
|
|
The conclusion I made with this are:-
lazy=true, then you need to write query to get data(The reason it come empty)
lazy=false, you will get data (don't know why)
it came empty because you've not open any session for it
I am still confused
|
 |
David Byron
Rancher
Joined: Jan 20, 2009
Posts: 168
|
|
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.
|
SCJD 6, Baroque Potion, Intermediate Java, G+
|
 |
Kaustubh G Sharma
Ranch Hand
Joined: May 13, 2010
Posts: 1145
|
|
Thanks David, I think you cleared lot of my confusion. Let me read more about it so that I can clear it fully. Cheers
|
 |
 |
|
|
subject: Lazyness in hibernate?
|
|
|