Skip to content

Commit 0c97bbd

Browse files
committed
talk about lack of lazy fetching in Jakarta Data
and about SS.fetch() Signed-off-by: Gavin King <gavin@hibernate.org>
1 parent 477be17 commit 0c97bbd

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

documentation/src/main/asciidoc/repositories/Repositories.adoc

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,27 @@ The following table contrasts the two programming models.
8787
| SQL execution | During flush | Immediate
8888
| Updates | Usually implicit (dirty checking during flush) | Always explicit (by calling `@Update` method)
8989
| Operation cascading | Depends on `CascadeType` | Never
90+
| Lazy fetching | Implicit | Explicit using link:{doc-javadoc-url}org/hibernate/StatelessSession.html#fetch(java.lang.Object)[`StatelessSession.fetch()`]
9091
| Validation of JPQL | Runtime | Compile time
9192
|===
9293

9394
The fundamental difference here that Jakarta Data does not feature stateful persistence contexts.
9495
Among other consequences:
9596

9697
- entity instances are always detached, and so
97-
- updates require an explicit operation.
98+
- updates require an explicit operation, and
99+
- there's no transparent lazy association fetching.
98100

99101
It's important to understand that a repository in Hibernate Data Repositories is backed by a `StatelessSession`, not by a Jakarta Persistence `EntityManager`.
100102

103+
[IMPORTANT]
104+
====
105+
There's only one portable way to fetch an association in Jakarta Data, and that's by using a JPQL `join fetch` clause, in a <<query-method,`@Query` annotation>>.
106+
The specification does not provide a portable way to fetch an association lazily.
107+
To fetch an association, we need to <<stateless-fetch,call the `StatelessSession` directly>>.
108+
This really isn't as bad as it sounds; overuse of lazy fetching is associated with poor performance due to many round trips to the database server.
109+
====
110+
101111
[NOTE]
102112
====
103113
A future release of Jakarta Data will feature repositories backed by Jakarta Persistence stateful persistence contexts, but this functionality did not make the cut for Jakarta Data 1.0.
@@ -270,6 +280,16 @@ default void refresh(Book book) {
270280
This is very useful when we need to gain direct access to the `StatelessSession` in order to take advantage of the full power of Hibernate.
271281
====
272282

283+
[[stateless-fetch]]
284+
[TIP]
285+
====
286+
A resource accessor method is also useful when we need to lazily fetch an association.
287+
[source,java]
288+
----
289+
library.session().fetch(book.authors);
290+
----
291+
====
292+
273293
Usually, of course, we want Jakarta Data to take care of interacting with the `StatelessSession`.
274294

275295
[[lifecycle-method]]

0 commit comments

Comments
 (0)