JPA

Understanding FetchType.EAGER and FetchType.LAZY in JPA

Introduction

In this blog post, we will explore two key fetching strategies used in Java Persistence API (JPA): FetchType.EAGER and FetchType.LAZY. We will discuss what they are, how they work, and when to use each one.

What is FetchType.EAGER?

FetchType.EAGER is a FetchType option in JPA that enables eager loading of data. This means that the related entities are fetched from the database at the same time as the parent entity.

@Entity
public class University {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @OneToMany(fetch = FetchType.EAGER)
    private List<Student> students;

    // getters and setters...
}

In this example, the students list is fetched from the database as soon as a University entity is loaded. This is because the fetch type of the @OneToMany relationship is set to FetchType.EAGER.

What is FetchType.LAZY?

FetchType.LAZY, on the other hand, enables lazy loading of data. This means that the related entities are not loaded from the database at the same time as the parent entity. Instead, they are loaded later on, when you access them in your code.

@Entity
public class University {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @OneToMany(fetch = FetchType.LAZY)
    private List<Student> students;

    // getters and setters...
}

In this example, the students list is not immediately fetched from the database when a University entity is loaded. Instead, the students list is loaded only when you access it in your code, like so:

List<Student> students = university.getStudents();

This is because the fetch type of the @OneToMany relationship is set to FetchType.LAZY.

When to Use FetchType.EAGER and FetchType.LAZY?

The choice between FetchType.EAGER and FetchType.LAZY depends on the specific needs of your application.

  • FetchType.EAGER can be useful when the related entities are frequently required. However, it can also lead to loading more data than necessary, which can impact application performance.
  • FetchType.LAZY can help improve performance by loading only the data that is immediately needed. However, it requires more careful handling of the persistence context to avoid LazyInitializationException.

Conclusion

In conclusion, understanding the difference between FetchType.EAGER and FetchType.LAZY is crucial when working with JPA. By choosing the right FetchType for your application’s needs, you can ensure efficient data loading and optimal application performance.


I hope this helps! Let me know if you have any other questions. 😊

Avatar

Neelabh

About Author

As Neelabh Singh, I am a Senior Software Engineer with 6.6 years of experience, specializing in Java technologies, Microservices, AWS, Algorithms, and Data Structures. I am also a technology blogger and an active participant in several online coding communities.

You may also like

Blog JPA

Transitioning from Java Persistence API (JPA) to Jakarta Persistence: A Comprehensive Guide

What is Jakarta Persistence? This is because the Java Persistence API (JPA) was renamed to Jakarta Persistence in 2019¹. The
Blog JPA

Understanding JPA and Hibernate in Spring Boot Applications

Introduction In this blog post, we will explore two key technologies used in Java applications for interacting with databases: the