Scrivio Interview library
ConceptIntermediate6 min read · updated 12 November 2026

What is the difference between a fetch graph and a load graph in JPA entity graphs?

Both are entity graphs passed as query hints, jakarta.persistence.fetchgraph and jakarta.persistence.loadgraph. With a fetch graph, the attributes in the graph are loaded eagerly and all others are treated as lazy; with a load graph, the attributes in the graph are loaded eagerly and the others keep the fetch type of their mapping. Spring Data's @EntityGraph uses FETCH by default and can be set to LOAD, and Hibernate may still load basic attributes eagerly whatever the graph says.

Why this question is asked

Entity graphs let us decide per query which associations to load, without writing join fetches by hand. They come in two flavours with names so similar that many developers use one when they meant the other, and the difference only shows when a mapping has an EAGER association. Interviewers ask this concept question to check that we know exactly what each hint does to the attributes outside the graph. In this answer, we will recap what an entity graph is, see the two hints side by side with an example, look at how Spring Data exposes them, and cover what Hibernate does with basic attributes.

1A Quick Recap of Entity Graphs

An entity graph is a description of which attributes of an entity, and of its associated entities, should be loaded by a query or a find. We can define it with @NamedEntityGraph on the entity, build it in code with em.createEntityGraph, or, in Spring Data, list attribute paths in @EntityGraph on a repository method. The persistence provider then adds the joins needed to load those attributes in the same query.

The graph says what to load. The hint name says what to do with everything the graph does not mention. That second part is the whole difference between the two kinds.

2The Two Hints Side by Side

Take an Order with a customer mapped @ManyToOne with the default EAGER, a shippingAddress mapped LAZY, and lines mapped LAZY. We pass a graph containing only lines, first as a fetch graph and then as a load graph.

Java
EntityGraph<Order> graph = em.createEntityGraph(Order.class);
graph.addAttributeNodes("lines");
Order a = em.find(Order.class, 7L, Map.of("jakarta.persistence.fetchgraph", graph));
// lines: loaded (in the graph)
// customer: lazy (not in the graph, EAGER mapping ignored)
// shippingAddress: lazy
em.clear();
Order b = em.find(Order.class, 7L, Map.of("jakarta.persistence.loadgraph", graph));
// lines: loaded (in the graph)
// customer: loaded (not in the graph, keeps its EAGER mapping)
// shippingAddress: lazy (not in the graph, keeps its LAZY mapping)
What happens to each attribute
Attribute and mappingIn graph?fetchgraphloadgraph
lines, LAZYyesloadedloaded
customer, EAGERnotreated as lazyloaded, mapping kept
shippingAddress, LAZYnolazylazy
id and versionalwaysloadedloaded

So for a fetch graph the rule is: the graph is the complete plan, and anything outside it is lazy. For a load graph the rule is: the graph adds eager attributes on top of the mapping. With a model where every association is already LAZY, both hints give the same result, which is why the difference often goes unnoticed. It appears exactly when the mapping has EAGER associations, and then the fetch graph is the way to switch them off for one query, which JPA otherwise does not allow. Keep in mind that the specification describes this as the intended semantics; whether an association outside a fetch graph is really left unloaded depends on the provider being able to make it lazy, so check the SQL.

3How the Hints Are Passed

Both are query hints, named options passed to find or to a query. In Jakarta Persistence 3 the names start with jakarta.persistence; older javax.persistence names belong to JPA 2 and pre-Jakarta stacks. The same hint works on a query.

Java
List<Order> orders = em.createQuery("select o from Order o where o.status = :s", Order.class)
.setParameter("s", Status.OPEN)
.setHint("jakarta.persistence.fetchgraph", graph)
.getResultList();

Notice that the query itself does not mention lines. The graph adds the join, so the same JPQL can serve different use cases with different graphs. Jakarta Persistence 3.2 also added more typed ways to pass graphs and options to find; check your version for what is available.

4Spring Data's @EntityGraph and Its Type

In Spring Data JPA, @EntityGraph on a repository method builds or references a graph and passes it as a hint. Its type attribute chooses the hint: EntityGraphType.FETCH, the default, uses the fetch graph semantics, and EntityGraphType.LOAD uses the load graph semantics.

Java
interface OrderRepository extends JpaRepository<Order, Long> {
@EntityGraph(attributePaths = "lines") // type = FETCH by default
List<Order> findByStatus(Status status);
@EntityGraph(attributePaths = "lines", type = EntityGraph.EntityGraphType.LOAD)
List<Order> findByCustomerId(Long customerId); // EAGER mappings still apply
}

So a developer who adds @EntityGraph(attributePaths = "lines") gets fetch graph behaviour without choosing it, and any EAGER association on Order that is not listed is treated as lazy for that method. That can be a pleasant surprise, fewer queries, or an unpleasant one, a LazyInitializationException in code that relied on the EAGER mapping. Knowing the default lets us predict which.

5Basic Attributes and Hibernate

The specification says that with a fetch graph, attributes outside the graph are treated as lazy. For basic attributes, simple columns like status or total, Hibernate normally loads them anyway. Lazy loading of individual columns needs bytecode enhancement, and without it there is nothing to gain from leaving a column out of a select that reads the row anyway. So entity graphs in practice control associations, not columns.

If we want a query to read fewer columns, the tool is a DTO projection, not an entity graph. Ids and version attributes are always loaded in both cases, because Hibernate needs them to manage the entity.

6Follow-up questions

When would you deliberately choose a load graph?

When the mapping's eager associations are intended and we only want to add one more association for a particular query. For example, if Order's customer is EAGER by design and a report also needs lines, a load graph with lines keeps the customer loaded without listing it again.

Can a graph include nested attributes?

Yes. With @NamedEntityGraph we use subgraphs, and with Spring Data's attributePaths we can write dotted paths such as lines.product. Each extra collection in the graph adds joins and rows, and two List collections in one graph hit the same MultipleBagFetchException as join fetches.

Does an entity graph work with pagination?

For to-one associations, yes. For collections, the same limit as fetch joins applies: Hibernate cannot limit rows in SQL without cutting collections, so it paginates in memory and warns. Paging ids first and then fetching details avoids that.

7Conclusion

In this answer, we saw that fetch graphs and load graphs are both entity graphs passed as hints, jakarta.persistence.fetchgraph and jakarta.persistence.loadgraph. Both load the attributes in the graph eagerly; a fetch graph treats every other attribute as lazy, while a load graph leaves other attributes with their mapped fetch type. Spring Data's @EntityGraph defaults to FETCH and can be set to LOAD, and Hibernate still loads basic attributes without bytecode enhancement.

What to say in the interviewAbout 80 seconds aloud

Open with

Both are entity graphs, a description of which attributes to load, passed to find or a query as a hint. The fetch graph hint is jakarta.persistence.fetchgraph and the load graph hint is jakarta.persistence.loadgraph.

Then, if they want more

They agree on the attributes in the graph: those are loaded eagerly, with joins added for us. They differ on everything else. With a fetch graph, attributes not in the graph are treated as lazy, even if the mapping says EAGER, so the graph is the complete plan for that query.

With a load graph, attributes not in the graph keep the fetch type from their mapping, so EAGER associations are still loaded and the graph only adds to them.

If all associations are already LAZY, the two behave the same, which is why people rarely notice; the difference shows when there are EAGER mappings, and the fetch graph is then the only standard way to switch them off for one query.

In Spring Data, @EntityGraph has a type attribute, FETCH by default or LOAD, so a plain @EntityGraph gives fetch graph semantics. One caveat: in Hibernate, basic attributes, simple columns, are loaded anyway unless bytecode enhancement is used, so entity graphs effectively control associations. If I want fewer columns, I use a DTO projection.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is the difference between a fetch graph and a load graph in JPA entity graphs?": https://scrivio-interview-library.netlify.app/databases/jpa-performance/2c0731e2ff/