CodesJava

Easy learning with example program codes

lazy vs eager loading in hibernate


Eager loading
Hibernate provides the facility of parent child relationship between entities. There may be the case when one parent record can have multiple child records. By default, in hibernate all its child records are fetched when a parent record is fetched. This is known as eager loading in java.

In the below example all subjects are fetched whenever a student object is fetched.

<list name="subjects" cascade="all" >
     <key column="Student_Id"/>
     <list-index column="idx"/>
     <one-to-many class="com.codesjava.business.Subject"/>
</list>

Lazy loading
As we know that hibernate supports inheritance which means records with parent child relationship. Consider the situation when one parent have multiple child records. In this case hibernate can lazy load all children, actually it will not load all children records when loading the parent. Hibernate will load all child records automatically on demand i.e. when accessed.

It is used to increase the performance.

By default lazy=”false” and all child records are loaded with parent. We can use lazy=”true” to support lazy loading.

<list name="subjects" cascade="all" lazy=true>
     <key column="Student_Id"/>
     <list-index column="idx"/>
     <one-to-many class="com.codesjava.business.Subject"/>
</list>
Sign Up/ Sign In
Ask a Question


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki