Lazy initialisation

What is lazy initialisation in hibernate

Questions by gdkundu

Showing Answers 1 - 21 of 21 Answers

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.

  Was this answer useful?  Yes

Ravi.M

  • Aug 21st, 2008
 

    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.

rajkr11

  • Aug 22nd, 2008
 

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.

  Was this answer useful?  Yes

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

  Was this answer useful?  Yes

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

inder_gwl

  • Sep 25th, 2009
 

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.

  Was this answer useful?  Yes

sambitsahu

  • Jan 6th, 2010
 

It decides whether the child object is loading to the parent or not.
--> We have to set it in the mapping file of the parent class.If lazy="true" (means not to load child)
-->By default the lazy loading is true.
-->But some cases we need to load the child object ,when the parent is loaded.
     So in that case we'll have to write lazy="false".

  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