← Java / Spring · Contents

JAVA-072 / Intermediate

N+1 queries and excessive exposure through JPA lazy relationships

A04:2021 Insecure Design · CWE-400 Uncontrolled Resource Consumption

Why is this a problem?

List requests trigger excessive queries and return unintended fields.

How to fix it

java
@OneToMany(fetch = FetchType.LAZY) List<Order> orders;
@EntityGraph(attributePaths = "orders") List<User> findAllWithOrders();

Further reading