Skip to content
Merged
Show file tree
Hide file tree
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
10 changes: 9 additions & 1 deletion documentation/src/main/asciidoc/introduction/Advanced.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1148,7 +1148,15 @@ session.enableFetchProfile(Book_.PROFILE_EAGER_BOOK);
Book eagerBook = session.find(Book.class, bookId);
----

Alternatively, an instance of link:{doc-javadoc-url}org/hibernate/EnabledFetchProfile.html[`EnabledFetchProfile`] may be obtained in a type safe way from the static metamodel, and used as a `FindOption`:
Alternatively, an instance of link:{doc-javadoc-url}org/hibernate/EnabledFetchProfile.html[`EnabledFetchProfile`] may be obtained in a type safe way from the static metamodel, and applied to the session:

[source,java]
----
Book_._EagerBook.enable(session);
Book eagerBook = session.find(Book.class, bookId);
----

Even better, the `EnabledFetchProfile` may be passed as a `FindOption`:

[source,java]
----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
package org.hibernate;

import jakarta.persistence.FindOption;
import org.hibernate.query.SelectionQuery;

/**
* A {@link jakarta.persistence.FindOption} which requests a named
* {@linkplain org.hibernate.annotations.FetchProfile fetch profile}.
* <p>
* An instance of this class may be obtained in a type safe way
* from the static metamodel for the class annotated
* {@link org.hibernate.annotations.FetchProfile @FetchProfile}
* and passed as an option to
* {@link Session#find(Class, Object, FindOption...) find()}.
* {@link org.hibernate.annotations.FetchProfile @FetchProfile}.
* <p>
* For example, this class defines a fetch profile:
* <pre>
Expand All @@ -28,11 +27,20 @@
* Set&lt;Author&gt; authors;
* }
* </pre>
* The fetch profile may be requested like this:
* <p>
* An {@code EnabledFetchProfile} may be obtained from the static
* metamodel for the entity {@code Book} and passed as an option to
* {@link Session#find(Class, Object, FindOption...) find()}.
* <pre>
* Book bookWithAuthors =
* session.find(Book.class, isbn, Book_._WithAuthors)
* </pre>
* Alternatively, it may be {@linkplain #enable(Session) applied}
* to a {@code Session} or {@code Query}.
* <pre>
* Book_._WithAuthors.enable(session);
* Book bookWithAuthors = session.find(Book.class, isbn);
* </pre>
* <p>
* When the static metamodel is not used, an {@code EnabledFetchProfile}
* may be instantiated directly, passing the name of the fetch profile
Expand All @@ -54,4 +62,20 @@
*/
public record EnabledFetchProfile(String profileName)
implements FindOption {

/**
* Enable the fetch profile represented by this
* object in the given session.
*/
public void enable(Session session) {
session.enableFetchProfile(profileName);
}

/**
* Enable the fetch profile represented by this
* object during execution of the given query.
*/
public void enable(SelectionQuery<?> query) {
query.enableFetchProfile(profileName);
}
}