FetchType is an enumeration in the Java Persistence API (JPA) that is used to define the fetching strategy for associations (relationships) between entities in a relational database.

There are two main values for FetchType:

This rule raises an issue when the fetch argument is explicitly set to FetchType.EAGER.

Why is this an issue?

Using FetchType.EAGER can lead to inefficient data loading and potential performance issues. Eager Loading initializes associated data on the spot, potentially fetching more data than needed.

How to fix it

Remove or replace FetchType.EAGER with FetchType.LAZY in JPA annotations.

Code examples

Noncompliant code example

@OneToMany(mappedBy = "parent", fetch = FetchType.EAGER) // Noncompliant
private List<ChildEntity> children;

@OneToMany(mappedBy = "child", fetch = FetchType.EAGER) // Noncompliant
private List<ParentEntity> parents;

Compliant solution

@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY) // Compliant
private List<ChildEntity> children;

@OneToMany(mappedBy = "child") // Compliant
private List<ParentEntity> parents;

Resources

Documentation