Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

futher explanation of "field-level lazy" #1419

Merged
merged 1 commit into from
Nov 4, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions documentation/src/main/asciidoc/reference/introduction.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,28 @@ first.

Similarly, field-level lazy fetching—an advanced feature, which
is only supported in conjunction with Hibernate's optional compile-time
bytecode enhancer—is also an explicit operation:
bytecode enhancer—is also an explicit operation.

To declare a lazy field we usually use the JPA `@Basic` annotation:

[source, JAVA, indent=0]
----
@Basic(fetch=LAZY) String isbn;
----

An optional one-to-one association declared `@OneToOne(fetch=LAZY)` is
also considered field-level lazy.

IMPORTANT: This annotation has no effect at all unless the entity is
processed by the bytecode enhancer during the build. Most Hibernate
users don't bother with this, since it's often an inconvenience.

TIP: On the other hand, if you're running Hibernate Reactive in Quarkus,
the bytecode enhancer is always enabled, and you won't even notice it's
there.

A lazy field is only fetched if we explicitly request it by calling an
overloaded version of the `fetch()` operation:

[source, JAVA, indent=0]
----
Expand All @@ -976,7 +997,9 @@ session.find(Book.class, book.id)
Note that the field to fetch is identified by a JPA metamodel `Attribute`.

TIP: We don't encourage you to use field-level lazy fetching unless you
have very specific requirements.
have very specific requirements. It's almost always more efficient to fetch
all the fields of an entity at once. Field-level lazy fetching is every bit
as vulnerable to N+1 selects as lazy association fetching.

=== Transactions

Expand Down