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.
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response