A JPA entity graph tells the persistence provider exactly which attributes and associations to load for one query or find call, overriding the fetch types declared in the mapping. Its main use is fixing the N+1 select problem, where one query for parents is followed by one extra query per parent to load a lazy association.
The feature is part of Jakarta Persistence (JPA 2.1 and later). Spring Data JPA exposes it through the @EntityGraph annotation on repository methods.
Associations should normally be lazy, so loading an Author does not load every Book. The cost appears when you read the lazy collection for many parents:
With 100 authors this runs 101 statements. Switching the mapping to FetchType.EAGER is the wrong fix: it applies to every query, including those that never need the books. An entity graph keeps the mapping lazy and changes the fetch plan only where you ask.
Declare a named graph on the entity with @NamedEntityGraph, then refer to it by name.
Note the annotation inside @NamedEntityGraph is @NamedAttributeNode. The original examples that use @AttributeNode do not compile. Use subgraphs to reach deeper, for example Author to books to reviews.
You pass a graph to find or to a query as a hint. The hint key decides how the graph is interpreted.
You can also build a graph in code, which avoids annotations on the entity:
| Hint | Attributes in the graph | Attributes not in the graph |
|---|---|---|
jakarta.persistence.fetchgraph | Treated as eager | Treated as lazy, even if mapped EAGER |
jakarta.persistence.loadgraph | Treated as eager | Keep the fetch type from the mapping |
Use a load graph to add associations on top of the mapping defaults, and a fetch graph when you want to be strict about what is loaded. Hibernate honours the specification for associations, but treats basic attributes as always loaded unless bytecode enhancement is enabled.
Spring Data JPA applies a graph to a derived query or @Query method with @EntityGraph.
The attributePaths form builds an ad hoc graph without a named declaration, which is often the simplest option. The default type is FETCH. The Hibernate log for either method shows a single select ... from author left join book instead of one query per author.
Both load an association in the same SQL statement. They differ in where the plan lives.
| Entity graph | JOIN FETCH in JPQL | |
|---|---|---|
| Defined in | Annotation, code or repository method | The query string |
| Reusable across queries | Yes | No, repeated per query |
Works with derived queries and find | Yes | Only with explicit JPQL |
| Join type | Outer join by default | Inner join unless LEFT JOIN FETCH |
Choose JOIN FETCH for a one-off query you are writing anyway, and an entity graph when the same plan applies to several access paths.
@BatchSize for the collection.List collections (bags) at once throws MultipleBagFetchException. Use Set, or load the collections in separate queries.distinct in JPQL where needed. Hibernate 6 removes duplicate parent entities from the result automatically.It removes it for the associations in the graph. Associations outside the graph can still be loaded lazily one by one, so check the SQL log or statistics.
EntityGraphType.FETCH. You can set type = EntityGraphType.LOAD to keep the mapping defaults for attributes not named in the graph.
Yes. Set the same hint on the TypedQuery created from the criteria query, or use Query.setHint with either graph key.
No. Keep associations lazy and use entity graphs or fetch joins where needed, because eager mappings load data for every query.
An entity graph is a reusable, per-query fetch plan that overrides mapped fetch types. Define it with @NamedEntityGraph or in code, apply it with the fetchgraph or loadgraph hint, or with @EntityGraph in Spring Data JPA. Watch for in-memory pagination and bag fetching limits, and verify the generated SQL.