beginnerSpring Data JPA & Hibernate

What is the N+1 query problem, and how does it show up with Spring Data JPA?

If you fetch a list of N parent entities and then access a lazy-loaded child collection on each one inside a loop, Hibernate issues one query for the parents plus one additional query per parent to fetch their children — N+1 queries total instead of 1 or 2. It's fixed with an explicit `JOIN FETCH` in a JPQL query or an `@EntityGraph`, which tells Hibernate to pull the association in the same query.

This is a Pro chapter

Sign in, then upgrade to Pro or Power to unlock this and the full Spring Ecosystem Mastery library.

What is the N+1 query problem, and how does it show up with Spring Data JPA?

Next Step

Continue to What's the difference between `save()` triggering an INSERT vs an UPDATE in Spring Data JPA?← Back to all Spring Boot questions