diff --git a/documentation/src/main/asciidoc/introduction/Advanced.adoc b/documentation/src/main/asciidoc/introduction/Advanced.adoc index 22aaa74b32fe..26c74a8e658b 100644 --- a/documentation/src/main/asciidoc/introduction/Advanced.adoc +++ b/documentation/src/main/asciidoc/introduction/Advanced.adoc @@ -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] ---- diff --git a/hibernate-core/src/main/java/org/hibernate/EnabledFetchProfile.java b/hibernate-core/src/main/java/org/hibernate/EnabledFetchProfile.java index 8b783d8f29a8..b26ed6b4c48f 100644 --- a/hibernate-core/src/main/java/org/hibernate/EnabledFetchProfile.java +++ b/hibernate-core/src/main/java/org/hibernate/EnabledFetchProfile.java @@ -5,6 +5,7 @@ package org.hibernate; import jakarta.persistence.FindOption; +import org.hibernate.query.SelectionQuery; /** * A {@link jakarta.persistence.FindOption} which requests a named @@ -12,9 +13,7 @@ *
* 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}. *
* For example, this class defines a fetch profile: *
@@ -28,11 +27,20 @@ * Set<Author> authors; * } *- * The fetch profile may be requested like this: + *
+ * 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()}. *
* Book bookWithAuthors = * session.find(Book.class, isbn, Book_._WithAuthors) *+ * Alternatively, it may be {@linkplain #enable(Session) applied} + * to a {@code Session} or {@code Query}. + *
+ * Book_._WithAuthors.enable(session); + * Book bookWithAuthors = session.find(Book.class, isbn); + **
* When the static metamodel is not used, an {@code EnabledFetchProfile} * may be instantiated directly, passing the name of the fetch profile @@ -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); + } }