@EntityGraph in Spring Data JPA — Declarative Eager Loading Per Query
Fast recall
Quick revision
Core answer
Whether you know per-query eager loading is the clean fix for N+1, without polluting entity metadata
Not knowing @EntityGraph exists and switching FetchType to EAGER globally on the entity class
Explain that @EntityGraph fixes N+1 for specific queries without touching the entity's global fetch strategy — the best of both worlds
Interview-ready guide
Interview answer
@EntityGraph(attributePaths = "customer") on findByStatus can request customers for an order list without making Order.customer globally eager. Hibernate commonly renders a join for a simple to-one attribute, but the annotation's contract is fetched availability, not SQL syntax. Nested collections can create large results or secondary operations. Use a DTO projection if the endpoint needs only a few fields.
@EntityGraph in Spring Data JPA — Declarative Eager Loading Per Query
Direct Answer
@EntityGraph on a Spring Data JPA method supplies a JPA entity graph for that query, asking the provider to treat named attributes according to fetchgraph or loadgraph semantics. It does not guarantee that Spring generates a JOIN or that all data arrives in exactly one SQL statement. It changes the fetch plan without changing the mapping globally.
Common Mistakes & Pitfalls
- Not knowing it exists — hitting N+1 and switching the entity to
FetchType.EAGER, polluting metadata and hurting performance elsewhere. - Wrong attributePaths — a typo like
"item"when the field isitemsfails or throws at runtime; rely on IDE autocomplete. - Overusing it — putting @EntityGraph on
findAll,findById, and every custom method effectively makes the association EAGER globally; apply it only where needed.
Check your understanding
Answer these without looking back at the article.
- What is the difference between @EntityGraph type FETCH and LOAD?
- Can you use @EntityGraph with a custom @Query?
- What is a named entity graph and how do you define one?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.