GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  J2EE  >  Hibernate
Go To First  |  Previous Question  |  Next Question 
 Hibernate  |  Question 29 of 33    Print  
lazy initialisation
what is lazy initialisation in hibernate


  
Total Answers and Comments: 6 Last Update: September 25, 2009     Asked by: gdkundu 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: Ravi.M
 
    Hibernate supports the feature of lazy initilasation for both entities and collections. What this actually means is, the Hibernate engine loads only those objects that we are querying for and doesnt try to fetch other entities(that are associated with the entity we are querying) or collections.
   
  An attribute 'lazy' can be used to let Hibernate know if the associated entity or collection has to be lazily loaded or prefetched.

Ex:
  

<set name="Children" lazy="false" inverse="true">

  <key column="FK_COL"/>

  <one-to-many class="Parent"/>

</set>

   This causes the collection to be eagerly fetched rather than doing a lazy fetch.  If on the other hand , the attribute value of lazy is set to true, then hibernate will not make an attempt to fire the query for fetching the collection object until the request is made by the user.

 Hope this answer helps.

Thanks
Ravi.

Above answer was rated as good by the following members:
JaiBharath, SitaPutta
May 19, 2008 13:36:51   #1  
arindam_0006in Member Since: August 2007   Contribution: 15    

RE: lazy initialisation
when loading an obj that has a collection hibernate loads collection elements ONLY when actually you ask for them; so this improves performance among other advantages.

when u use hibernate u can make lazy true or lazy false.

 
Is this answer useful? Yes | No
August 21, 2008 04:39:45   #2  
Ravi.M Member Since: August 2008   Contribution: 1    

RE: lazy initialisation
Hibernate supports the feature of lazy initilasation for both entities and collections. What this actually means is the Hibernate engine loads only those objects that we are querying for and doesnt try to fetch other entities(that are associated with the entity we are querying) or collections.

An attribute 'lazy' can be used to let Hibernate know if the associated entity or collection has to be lazily loaded or prefetched.

Ex:

<set name "Children" lazy "false" inverse "true">

<key column "FK_COL"/>

<one-to-many class "Parent"/>

</set>

This causes the collection to be eagerly fetched rather than doing a lazy fetch. If on the other hand the attribute value of lazy is set to true then hibernate will not make an attempt to fire the query for fetching the collection object until the request is made by the user.

Hope this answer helps.

Thanks
Ravi.

 
Is this answer useful? Yes | NoAnswer is useful 2   Answer is not useful 0Overall Rating: +2    
August 22, 2008 00:18:11   #3  
rajkr11 Member Since: January 2007   Contribution: 6    

RE: lazy initialisation
Lazy initialization load the child objects while loading parent object. To getting this set the lazy false in mapping file or class.lazy false in class file and hibernate will load the child when parent is loaded from the database. by default lazy is true.
 
Is this answer useful? Yes | No
August 22, 2008 07:44:07   #4  
Karuna Reddy Member Since: June 2007   Contribution: 40    

RE: lazy initialisation

Lazy false by default so the parent table can fetch the fields from child table also

Ex: A is a class which have sub class B

If
you want to access the fields from both A and B then you have to write in A class as laze false (by default)
when
you write laze true then it won't fetch the fields from the B class
If you have any comments please let me know


 
Is this answer useful? Yes | No
August 22, 2008 08:13:16   #5  
Karuna Reddy Member Since: June 2007   Contribution: 40    

RE: lazy initialisation
by default lazy true so that parent table wont fetch the fields from child table. so in that case u have to wite explicitly lazy false
Then we can fetch the fields from the psrent as well as child fileds

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
September 25, 2009 03:29:16   #6  
inder_gwl Member Since: September 2009   Contribution: 3    

RE: lazy initialisation
Lazy loading means that any foreign key references that you have in your table will be loaded only when referred to by the application. Eager loading means everything will be loaded at once.
 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape