Skip to content

Commit 8ddbb03

Browse files
committed
document EntityGraphs
1 parent 56ffbc1 commit 8ddbb03

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

documentation/src/main/asciidoc/introduction/Interacting.adoc

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,72 @@ We're getting a bit ahead of ourselves here, but let's quickly mention the gener
337337
Instead, fetch all the data you'll need upfront at the beginning of a unit of work, using one of the techniques described in <<association-fetching>>, usually, using _join fetch_ in HQL.
338338
====
339339

340+
It's clear we need a way to request that an association be _eagerly_ fetched using a database `join`.
341+
One way to do that is by passing an `EntityGraph` to `find()`.
342+
343+
[[entity-graph]]
344+
=== Entity graphs and eager fetching
345+
346+
When an association is mapped `fetch=LAZY`, it won't, by default, be fetched when we call the `find()` method.
347+
We may request that an association be fetched eagerly (immediately) by passing an `EntityGraph` to `find()`.
348+
349+
The JPA-standard API for this is a bit unwieldy:
350+
351+
[source,java]
352+
----
353+
var graph = entityManager.createEntityGraph(Book.class);
354+
graph.addSubgraph(Book_.publisher);
355+
entityManager.find(Book.class, bookId, Map.of(SpecHints.HINT_SPEC_FETCH_GRAPH, graph));
356+
----
357+
358+
This is untypesafe and a bit verbose.
359+
Hibernate has a better way:
360+
361+
[source,java]
362+
----
363+
var graph = session.createEntityGraph(Book.class);
364+
graph.addSubgraph(Book_.publisher);
365+
session.byId(Book.class).fetching(graph).load(bookId);
366+
----
367+
368+
This code adds a `left outer join` to our SQL query, fetching the associated `Publisher` along with the `Book`.
369+
370+
We may even attach additional nodes to our `EntityGraph`:
371+
372+
[source,java]
373+
----
374+
var graph = session.createEntityGraph(Book.class);
375+
graph.addSubgraph(Book_.publisher);
376+
graph.addPluralSubgraph(Book_.authors).addSubgraph(Author_.person);
377+
session.byId(Book.class).fetching(graph).load(bookId);
378+
379+
----
380+
381+
This results in a SQL query with _four_ ``left outer join``s.
382+
383+
[NOTE]
384+
====
385+
In the code examples above, we're making use of the JPA static metamodel.
386+
The classes `Book_` and `Author_` are generated by the annotation processor we have in our <<hello-hibernate,gradle build>>, one of the <<optional-dependencies,optional dependencies>> of Hibernate.
387+
They let us refer to attributes of our model in a completely type-safe way.
388+
We'll use them again, below, when we talk about <<criteria-queries>>.
389+
====
390+
391+
JPA specifies that any given `EntityGraph` may be interpreted in two different ways.
392+
393+
- A _fetch graph_ specifies exactly the associations that should be eagerly loaded.
394+
Any association not belonging to the entity graph is proxied and loaded lazily only if required.
395+
- A _load graph_ specifies that the associations in the entity graph are to be fetched in addition to the associations mapped `fetch=EAGER`.
396+
397+
You're right, the names make no sense.
398+
But don't worry, if you take our advice, and map your associations `fetch=LAZY`, there's no difference between a "fetch" graph and a "load" graph, so the names don't matter.
399+
400+
[NOTE]
401+
====
402+
JPA even specifies a way to define named entity graphs using annotations.
403+
But the annotation-based API is so verbose that it's just not worth using.
404+
====
405+
340406
[[flush]]
341407
=== Flushing the session
342408

documentation/src/main/asciidoc/introduction/Mapping.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ So we don't usually need to explicitly specify that a column should be of type `
559559
The constant values defined in the class `org.hibernate.Length` are very helpful here:
560560

561561
.Predefined column lengths
562-
[%autowidth.stretch]
562+
[cols="10,12,~"]
563563
|===
564564
| Constant | Value | Description
565565

0 commit comments

Comments
 (0)