• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Lazyness in hibernate?

 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check this may help
 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 175
Clojure Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks David, I think you cleared lot of my confusion. Let me read more about it so that I can clear it fully. Cheers
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic