0
0
Spring Bootframework~5 mins

Fetch types (LAZY vs EAGER) in Spring Boot

Choose your learning style9 modes available
Introduction

Fetch types decide when related data is loaded from the database. They help control performance and memory use.

When you want to load related data only when needed to save resources.
When you want to load all related data immediately for quick access.
When working with large data sets and want to avoid slow queries.
When you want to avoid unexpected database calls during object use.
When designing APIs that return nested data and want control over loading.
Syntax
Spring Boot
@OneToMany(fetch = FetchType.LAZY)
private List<Item> items;

@OneToMany(fetch = FetchType.EAGER)
private List<Item> items;

Use fetch = FetchType.LAZY to load data only when accessed.

Use fetch = FetchType.EAGER to load data immediately with the main entity.

Examples
Orders are loaded only when you call getOrders().
Spring Boot
@OneToMany(fetch = FetchType.LAZY)
private List<Order> orders;
Customer data loads immediately with the main entity.
Spring Boot
@ManyToOne(fetch = FetchType.EAGER)
private Customer customer;
Profile loads only when accessed, saving resources if unused.
Spring Boot
@OneToOne(fetch = FetchType.LAZY)
private Profile profile;
Sample Program

This example shows a User with many Posts. Posts load only when accessed (LAZY). Each Post loads its User immediately (EAGER). This balances performance and convenience.

Spring Boot
import jakarta.persistence.*;
import java.util.List;

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

    private String name;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
    private List<Post> posts;

    public User() {}

    public User(String name) {
        this.name = name;
    }

    public List<Post> getPosts() {
        return posts;
    }

    public String getName() {
        return name;
    }
}

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

    private String content;

    @ManyToOne(fetch = FetchType.EAGER)
    private User user;

    public Post() {}

    public Post(String content, User user) {
        this.content = content;
        this.user = user;
    }

    public String getContent() {
        return content;
    }

    public User getUser() {
        return user;
    }
}

// Explanation:
// When loading a User, posts are not loaded immediately (LAZY).
// When loading a Post, the User is loaded immediately (EAGER).
// This helps avoid loading all posts unless needed, but always loads the user with a post.
OutputSuccess
Important Notes

LAZY loading can cause errors if accessed outside a database session.

EAGER loading can slow down queries if many related objects exist.

Choose fetch type based on how and when you need related data.

Summary

Fetch types control when related data loads: LAZY delays loading, EAGER loads immediately.

Use LAZY to save resources and EAGER for quick access to related data.

Pick the right fetch type to balance performance and convenience in your app.