Friday, November 6, 2009

Hibernate Lazy Initialization

Lazy Initialization in Hibernate

Hibernate supports the feature of lazy initilasation for both entities and collections, which actually means is, the Hibernate engine loads only those objects that we are querying for and doesn't try to fetch other entities (that are associated with the entity we are querying) or collections.


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. 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.


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

<set name="Child" lazy="false" inverse="true">
<key column="FOREIGN_KEY_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 fetchingthe collection object until the request is made by the user.

0 comments:

Blog Widget by LinkWithin

JS-Kit Comments

  © Blogger template Newspaper III by Ourblogtemplates.com 2008

Back to TOP