What is lazy fetching in hibernate

Showing Answers 1 - 17 of 17 Answers

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.

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

  Was this answer useful?  Yes

narasimha

  • Dec 18th, 2006
 

Simply saying: It maintains the relactionship between two tables.

  Was this answer useful?  Yes

sony V george

  • Apr 4th, 2007
 

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.

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.

  Was this answer useful?  Yes

Lazy fetching means for example in hibernate if we use load() method then load() is lazy fetching i.e it is not going to touch the database until we write empobject.getString("eno");So when we write above statement in that instance it touch the database and get all the data from database.It is called as lazy loading.If we see another example i.e session.get(...) method is used then at that instance it is going to touch the database and get the data and place the data in session object it is called as eager loading

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions