@NamedEntityGraph is a JPA annotation (jakarta.persistence.NamedEntityGraph) that declares, on an entity class, a reusable set of attributes and relationships to load together in one query. It is the standard tool for fixing lazy-loading problems and the N+1 select problem without changing the fetch type on the mapping.
You define the graph once, give it a name, and apply it per query as a fetch graph or a load graph. Spring Data JPA exposes this with the @EntityGraph annotation on repository methods.
Associations are normally mapped lazily, which is the safe default. If a screen needs an order together with its customer and items, the persistence provider issues one query for the orders and then one more per association access. For 100 orders that is 1 + 100 or more queries (the N+1 problem). Switching the mapping to EAGER fixes this screen but penalises every other query. An entity graph lets you choose the fetch plan for each use case.
The annotation sits on the entity. attributeNodes lists the attributes to load, and subgraphs describes the attributes of related entities.
This graph loads the order, its customer, its items, and the product of each item. To declare several graphs on one entity, use @NamedEntityGraphs or repeat @NamedEntityGraph, which is repeatable since JPA 2.2.
Pass the graph as a query or find hint. The hint name decides the semantics:
| Hint | Meaning |
|---|---|
jakarta.persistence.fetchgraph | Attributes in the graph are fetched eagerly; all others are treated as lazy |
jakarta.persistence.loadgraph | Attributes in the graph are fetched eagerly; others keep the fetch type from the mapping |
Older code uses the javax.persistence prefix on Java EE and Spring Boot 2.
Reference the graph by name on a repository method:
You can also skip the entity annotation and write an ad hoc graph on the repository method with @EntityGraph(attributePaths = {"customer", "items"}). That is often simpler when the graph is used in only one place.
MultipleBagFetchException in Hibernate when both are List without an order column. Fetch one collection per query, change one to a Set, or load the second with batch fetching.| Approach | Strength | Weakness |
|---|---|---|
JOIN FETCH in JPQL | Explicit and visible in the query | The query text is tied to one fetch plan |
@NamedEntityGraph | Reusable plan, attached by name | Defined away from the query |
@EntityGraph attributePaths | Short and local in Spring Data | Not reusable by name |
With fetchgraph, anything not in the graph is lazy. With loadgraph, anything not in the graph keeps its mapped fetch type, so an EAGER attribute outside the graph is still loaded.
It does for the associations listed in the graph. Associations outside the graph still load lazily and can cause N+1 behaviour if you access them in a loop.
Yes. Call entityManager.createEntityGraph(Order.class) and add nodes with addAttributeNodes and addSubgraph.
Entity graphs are part of the JPA specification since 2.1, so Hibernate, EclipseLink and other compliant providers support them.
@NamedEntityGraph declares a named fetch plan on an entity. Apply it with the fetchgraph or loadgraph hint, or with Spring Data's @EntityGraph, to load exactly the associations a use case needs in one query. Keep graphs narrow and watch for multiple bags and in-memory pagination.