A JPA collection mapping is fetched eagerly, so every load of the entity also loads the whole collection
What it finds
A collection mapping with fetch = FetchType.EAGER makes JPA load the whole collection whenever the owning entity is loaded, whether the code uses it or not: one more query or a wide join for every entity of every result, and with several eager collections a cartesian product of their rows. Lazy loading, with a fetch join or an entity graph where a query needs the collection, loads it only then.
Reported is fetch = FetchType.EAGER on @OneToMany, @ManyToMany or @ElementCollection, in a file that imports the persistence annotations. Single-valued associations, which are eager by default, are left alone.
From the rule's source, eager_collection_fetch.rs.
import java.util.List;import javax.persistence.Entity;import javax.persistence.FetchType;import javax.persistence.ManyToOne;import javax.persistence.OneToMany; @Entityclass Customer { @OneToMany(mappedBy = "customer", fetch = FetchType.EAGER)tx0 reports: this @OneToMany collection is loaded with every load List<Object> orders; @OneToMany(mappedBy = "customer") List<Object> invoices; @OneToMany(fetch = FetchType.LAZY) List<Object> notes; @ManyToOne(fetch = FetchType.EAGER) Object region;} Turn it on, off or suppress it
The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/java:eager-collection-fetch" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/java:eager-collection-fetch] reason="..."tx0 explain tx0/java:eager-collection-fetch shows the same in the terminal. Found a false positive? Report it.