Resolved Question
what is lazy fetching in hibernate
 
Details:
  asked by: lavastar  on: Aug 18, 2009  
 Best Answer ! ! !
clocking 's Answer  ( this answer is maked as best answer of this question at: Aug 27, 2009 )


Lazy setting decides whether to load child objects while loading the Parent Object.You need to do this setting respective hibernate mapping file of the parent class.Lazy = true (means not to load child)By default the lazy loading of the child objects is true. This make sure that the child objects are not loaded unless they are explicitly invoked in the application by calling getChild() method on parent.In this case hibernate issues a fresh database call to load the child when getChild() is actully called on the Parent object.But in some cases you do need to load the child objects when parent is loaded. Just make the lazy=false and hibernate will load the child when parent is loaded from the database.Exampleslazy=true (default)Address child of User class can be made lazy if it is not required frequently.lazy=falseBut you may need to load the Author object for Book parent whenever you deal with the book for online bookshop.
1 comments  answered on: Aug 26, 2009 
Other Answer (5)

Hi

Lazy fetching is related to loading of the Child objects for its parent ( In the terms of databse its primary key-foreign key relationship). In hbm.xml file you have to specify whether you want to load the child objects while loading the parent. By default hibernate doesn't load the whole child objects (lazy true ).

But Sometimes it doesn't work properly it doesn't load the child objects for the parent.and second problem is that if you have condition like ....

Employee Table -1-----n-> Emp_Dept Table<-n------1- Department Table

And you have many to many relationship between them then by specifying lazy false for both the parent object(Employee And Department) hibernate try to get all the child for this parent and for this child. Now this child becomes the parent and hibernate will try to get all the child for this parent. This process may continue.This results in performance issue.

So U have to take care while writing the mapping files in case of many-to-many relationships.

Thanks

Kuldeep

  tortoise18   answered on£ºAug 19, 2009  0 comments

Simply saying: It maintains the relactionship between two tables.
  ajay   answered on£ºAug 20, 2009  0 comments

There are two types of Loading in application. Eager loading and Lazy
loading. In eager loading we will fetch all the values from the Persistent storage
and cache it. It will make serious performance issues. There we use lazy
loading to avoid that scenario.

Some times we don't need to load the child
table values In that case we have to us lazy true in .hbm file. so hibernate
will fetch only parent table values. Internally it will load the wrapper class
but it does not cache all the values till we perform operation.
Main
Advantage: It will avoid caching unnecessary values and improve performances.
  seller1111   answered on£ºAug 22, 2009  0 comments

Disadvantage is LazyInitialization exception happens when the session is not available and a lazily associated is accesed.
  addictedt0u   answered on£ºAug 24, 2009  0 comments

Lazy loading is the responsibility is that to load the objects for its parent
In mapping file if
you want to load the child objects while loading the parent it will wont load coz by default load true
so
you have to specify lazy false
here child class become parent while loading the objects.
  thechosen   answered on£ºAug 26, 2009  0 comments

Hot Questions

Contact Us - IT-Interview