Table of contents
1.
Introduction
2.
Lazy Loading
3.
Eager Loading
4.
Advantages of Lazy and Eager Loading
4.1.
Lazy Loading
4.2.
Eager Loading
5.
Disadvantages of Lazy and Eager Loading
5.1.
Lazy Loading
5.2.
Eager Loading
6.
Lazy vs Eager loading in Hibernate
7.
Frequently Asked Questions
7.1.
How can a Hibernate immutable class be created?
7.2.
What does hibernate's lazy loading entail?
7.3.
What is ORM?
7.4.
What is JPQL?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lazy vs Eager Loading in Hibernate

Author Ayush Mishra
2 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hibernate is a free -to- download, open-source Object/Relational persistence and query service that is covered by the GNU Lesser General Public License. Hibernate provides data retrieval and querying facilities in addition to managing the mapping of Java classes to database tables (and Java data types to SQL data types).

Data fetching and loading techniques when using an ORM can be divided into two categories: eager and lazy.

In this article, we will discuss Eager and Lazy and point out differences between Lazy vs Eager loading in hibernate and show those can be used in Hibernate.

Lazy Loading

In this context of “Lazy vs Eager Loading in hibernate,” will know about Lazy loading. It is a fetching technique for all entities. While loading the parent class object, it chooses whether to load a child class object. The fetching method must be defined when using association mapping in Hibernate. It is mostly used to retrieve required objects from the database.

For instance, we have a parent class that has a number of child classes. Now that lazy loading is an option; Hibernate will only load the necessary classes and not all of them. Since the object is only loaded once when necessary, it avoids a heavy load. By avoiding pointless work and lowering memory requirements, lazy loading enhances performance.

<set name="orderDetail" table="USER_ORDER" inverse="true" lazy="true" fetch="select">
    <key>
        <column name="USER_ID" not-null="true" />
    </key>
   <one-to-many class="com.codingNinjas.hibernate.fetching.model.OrderDetail" />
</set>

 

By default, lazy="false," and the parent record is loaded before any child records. To facilitate lazy loading, we can use lazy="true."

Eager Loading

In this part of “Lazy vs Eager Loading in hibernate,” will know about Eager loading. The parent-child connection between entities is a feature offered by Hibernate. It is possible for a parent record to have more than one kid record. When a parent record is fetched in hibernate, all of its child records are also conveyed. Java refers to this as eager loading.

<set name="orderDetail" table="USER_ORDER" inverse="true" lazy="false" fetch="select">
    <key>
        <column name="USER_ID" not-null="true" />
    </key>
   <one-to-many class="com.baeldung.hibernate.fetching.model.OrderDetail" />
</set>

 

This is the process for enabling lazy loading. The simple formula lazy = "false" will switch on eager loading and turn off lazy loading.

Advantages of Lazy and Eager Loading

In this context of “Lazy vs Eager loading in Hibernate,” we will look at various advantages of Lazy and Eger loading:-

Lazy Loading

  • Much quicker initial load time than with the alternative method
  • Less memory is used compared with the alternative method.

Eager Loading

  • There are no performance consequences due to delayed initialization.
  • It is a good practice to reduce further queries on the Server.

Disadvantages of Lazy and Eager Loading

In this context of “Lazy vs Eager loading in Hibernate,” we will look at various disadvantages of Lazy and Eger loading:-

Lazy Loading

  • Delayed initialization could have an unfavorable effect on performance.
  • Lazily initialized objects must occasionally be handled carefully, or you risk an error.

Eager Loading

  • The initial loading time was too long.
  • Performance may be impacted if too much-unneeded data is loaded.

Lazy vs Eager loading in Hibernate

Let's have a look at the comparison table of Lazy and Eager loading to understand the differences between them.

It compare the difference  between eager and lazy loading in hibernate

Frequently Asked Questions

How can a Hibernate immutable class be created?

A class will be considered immutable if you set mutable="false" on it. It is mutable="true" by default.

What does hibernate's lazy loading entail?

Hibernate's lazy loading enhances performance. The child objects are loaded as needed.

Lazy loading is now enabled by default in Hibernate 3; therefore lazy="true" is unnecessary. It refers to not loading the child's objects while loading the parent.

What is ORM?

Object Relational Mapping is referred to as ORM. It is a programming paradigm that is used to persist Java objects to database tables. It converts data between type systems that cannot coexist within relational databases and OOP languages.

What is JPQL?

The JPA specification is the Java Persistence Query Language (JPQL). When constructing the query, it is used to perform database operations. The SQL queries are executed by JPQL using the entity object model.

Conclusion

Congrats on finishing the blog! This blog covers all the necessary points about Lazy vs Eager loading in Hibernate. We also discussed the various advantages and disadvantages of Lazy and Eager loading. As a windup, we discussed some differences between Lazy vs Eager loading in Hibernate.

If you want to explore more blogs like “Lazy vs Eager Loading in hibernate,” please follow these blogs especially curated for readers like you - Hibernate Java FrameworkJDBC ConnectionGuide to ORM, Introduction to Spring Boot.

Recommended reading: 

Difference Between Compiler and Interpreter and Assembler
Please refer to our guided pathways on Code studio to learn more about DSACompetitive ProgrammingJavaScriptSystem Design, etc. Enroll in our courses, and use the accessible sample exams and questions as a guide. For placement preparations, look at the interview experiences and interview package.

Please do upvote our blogs if you find them helpful and informative!

Happy learning!

Live masterclass