Differences between Fetch Type LAZY and EAGER in Hibernate

About Fetch Type

When expressing a relationship in Hibernate, add the following annotations such as @OneToMany.

User.java


@Getter
@Setter
@NoArgsConstructor
@Entity
public class User {
    public User(Long id, String name, Integer age, Set<Post> posts){
        this.id = id;
        this.name = name;
        this.age = age;
        this.posts = posts;
    }
    private Long id;
    private String name;
    private Integer age;

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "user" )
    private Set<Post> posts = new HashSet<>();
}

Posts are annotated with @OneToMany. Either FetchType.EAGER or FetchType.LAZY can be specified in fetch of this argument. By the way, if not specified, it defaults to LAZY. So what's the difference between the two?

Difference between LAZY and EAGER

In the case of LAZY, Posts will not be loaded when the User instance is called in Hibernate. In other words, user.getPosts () will be Null. On the other hand, in the case of EAGER, the object with the relation is also read when the instance is called, so user.getPosts () is not Null either.

Each feature

LAZY: You need to be careful about Null, but you can use memory efficiently. EAGER: Not null, but heavy load.

Use properly

The default LAZY is recommended for Spring Boot. In the previous example, it is possible that Posts will be displayed only when the User logs in, so it seems preferable to prepare a separate Repository class and specify the read conditions.

Conclusion

LAZY is basically recommended, but sometimes it is useful to use EAGER to prevent NullPointerExceptions errors.

References

https://howtoprogramwithjava.com/hibernate-eager-vs-lazy-fetch-type/

Recommended Posts

Differences between Fetch Type LAZY and EAGER in Hibernate
[Understanding] Differences between hashes and arrays in Ruby
Differences between IndexOutOfBoundsException and ArrayIndexOutOfBoundsException
Differences between namespace, module, scope and as in Rails routing
Think about the differences between functions and methods (in Java)
Differences in how to handle strings between Java and Perl
Differences between "beginner" Java and Kotlin
Difference between primitive type and reference type
Differences between Java and .NET Framework
Difference between final and Immutable in Java
Differences between preface and postfix of operators
[Java] Differences between instance variables and class variables
How to map tsrange type in Hibernate
Basic CRUD comparison between Mybatis and Hibernate
Difference between getText () and getAttribute () in Selenium
Difference between "|| =" and "instance_variable_defined?" In Ruby memoization
Difference between EMPTY_ELEMENTDATA and DEFAULTCAPACITY_EMPTY_ELEMENTDATA in ArrayList
Differences between Ruby strings and symbols [Beginner]
Difference between int and Integer in Java
[Java] Difference between equals and == in a character string that is a reference type
Organize your own differences in writing comfort between Java lambda expressions and Kotlin lambda expressions.
Differences between Spring Initializr packaging JAR and WAR
Difference between next () and nextLine () in Java Scanner
Differences in writing Java, C # and Javascript classes
Summarize the differences between C # and Java writing
Distinguish between positive and negative numbers in Java
Ruby: Differences between class methods and instance methods, class variables and instance variables