Lazy Loading in Hibernate

Lazy loading is a fetching technique used for all the entities in Hibernate. It decides whether to load a child class object while loading the parent class object. When we use association mapping in Hibernate, it is required to define the fetching technique. The main purpose of lazy loading is to fetch the needed objects from the database.

For example, we have a parent class, and that parent has a collection of child classes. Now, Hibernate can use lazy loading, which means it will load only the required classes, not all classes. It prevents a huge load since the entity is loaded only once when necessary. Lazy loading improves performance by avoiding unnecessary computation and reduce memory requirements.

Lazy loading can be used with all types of Hibernate mapping, i.e., one-to-one, one-to-many, many-to-one, and many-to-many.

Syntax of Lazy Loading

To enable Lazy loading, we use the following annotation parameter:

fetch= FetchType.LAZY

Following code is the syntax of lazy loading:

@OneToOne(fetch= FetchType.LAZY)

Example of Lazy Loading

  1. Create all POJO classes

In this step, we are going to develop persistent classes, i.e., and

 import java.util.Set;
 import javax.persistence.CascadeType;
 import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.Id;
 import javax.persistence.JoinColumn;
 import javax.persistence.JoinTable;
 import javax.persistence.ManyToMany;
 import javax.persistence.Table;
 public class Categories {
             private int cate_id;
             private String cate_name;
             @ManyToMany(targetEntity=Items.class, cascade=CascadeType.ALL, fetch = FetchType.LAZY)
             private Set items;
             public int getCate_id() {
                         return cate_id;
             public void setCate_id(int cate_id) {
                         this.cate_id = cate_id;
             public String getCate_name() {
                         return cate_name;
             public void setCate_name(String cate_name) {
                         this.cate_name = cate_name;
             public Set getItems() {
                         return items;
             public void setItems(Set items) {
                         this.items = items;

 import java.util.Set;
 import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.Id;
 import javax.persistence.ManyToMany;
 import javax.persistence.Table;
 public class Items {
             private int item_id;
             private String item_name;
             @ManyToMany(targetEntity=Categories.class,mappedBy="items", fetch = FetchType.LAZY)
             private Set categories;
             public int getItem_id() {
                         return item_id;
             public void setItem_id(int item_id) {
                         this.item_id = item_id;
             public String getItem_name() {
                         return item_name;
             public void setItem_name(String item_name) {
                         this.item_name = item_name;
             public Set getCategories() {
                         return categories;
             public void setCategories(Set categories) {
                         this.categories = categories;
  • Create the configuration file

In this step, we are going to create the configuration class (hibernate.cfg.xml) which contains the information of POJO class and the database.


<pre class="wp-block-preformatted"        update  org.hibernate.dialect.MySQL5Dialect com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/test root</property>                        root</property>                           
  • Create the main class that stores the object of the persistent class

In this step, we are going to create a class which consists of main() method, and stores the objects of POJO classes.

 import java.util.HashSet;
 import java.util.Set;
 import org.hibernate.Session;
 import org.hibernate.SessionFactory;
 import org.hibernate.Transaction;
 import org.hibernate.cfg.Configuration;
 public class App 
     public static void main( String[] args )
        Configuration cfg= new Configuration();
        SessionFactory factory = cfg.buildSessionFactory();
        Session session= factory.openSession();
        Categories cate1 = new Categories();
        cate1.setCate_name("category 01");
        Categories cate2 = new Categories();
        cate2.setCate_name("category 02");
        Items i= new Items();
        i.setItem_name("item 11");
        Items i2 =new Items();
        i2.setItem_name("item 21");
        Set set= new HashSet();
        Transaction t= session.beginTransaction();;;
        System.out.println("saved successfully!!!!");
  • Output
Lazy Loading in Hibernate
  • Database tables

categories table

Lazy Loading in Hibernate 1

items table

Lazy Loading in Hibernate 2

cate_items table

Lazy Loading in Hibernate 3