JPA Fetch Strategy Best Practices — Lazy by Default, Eager Per Query
Fast recall
Quick revision
Core answer
Whether you can articulate a sound fetch-strategy principle and justify it with concrete trade-offs
Switching associations to EAGER globally to dodge LazyInitializationException — curing the symptom, not the cause
State the principle crisply — lazy by default, eager per query — with concrete examples of when to apply each
Interview-ready guide
Interview answer
A list endpoint may need only Order id, status, and customer name, while a detail endpoint needs items as well. One global graph cannot serve both efficiently. Keep collections lazy and choose an explicit graph or projection for each query. Where practical with your provider, requesting LAZY for to-one associations can also avoid unwanted work, but proxy and bytecode behavior matter. Never fix N+1 by changing every relationship to EAGER; eager associations can themselves be loaded through secondary queries.
JPA Fetch Strategy Best Practices — Lazy by Default, Eager Per Query
Direct Answer
Prefer narrow entity mappings and request the data needed by each use case with fetch joins, entity graphs, projections, or batch fetching. Do not make every association globally EAGER. Also do not state that every relationship is LAZY by default: the JPA defaults are EAGER for @ManyToOne/@OneToOne and LAZY for collections, and LAZY is only a hint in the specification.
Common Mistakes & Pitfalls
- Switching to EAGER globally — the biggest mistake after a LazyInitializationException; it fixes one case but over-fetches everywhere through a global hammer for a local problem.
- Ignoring the root cause — the exception means you accessed lazy data outside a transaction; fix the boundary or JOIN FETCH, don't go EAGER.
- Over-fetching with @EntityGraph — adding it everywhere wastes resources on methods that usually need only the parent; profile and optimize where it matters.
Check your understanding
Answer these without looking back at the article.
- What are DTO projections in Spring Data JPA and how do they help performance?
- How do you decide between @EntityGraph and JOIN FETCH?
- What is the Open Session in View anti-pattern?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.