Skip to content
Preptima
mediumConceptScenarioMidSeniorStaff

This endpoint logs one query per row. Why are you seeing N+1 queries, and how do you fix them?

6 min readUpdated 2026-08-15

Answer

Short answer

You get N+1 when one query loads N rows and something later dereferences a lazy association on each of them, firing a select apiece. Fix it per call site with join fetch, an entity graph, or batch fetching - not by changing mappings to EAGER.

Was this answer helpful?

Likely follow-ups

  • How would you page over authors and still load their books in a bounded number of queries?
  • What does spring.jpa.open-in-view do, and why does leaving it enabled hide this class of bug until production?
  • When can you touch a lazy @ManyToOne without hitting the database at all?
  • Why does join-fetching two List collections in one query fail, and what changes if one of them is a Set?

Related questions

Further reading