Thanks to visit codestin.com
Credit goes to github.com

Skip to content

properly document EnabledFetchProfile #10165

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

Merged
merged 1 commit into from
May 16, 2025
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
9 changes: 8 additions & 1 deletion documentation/src/main/asciidoc/introduction/Advanced.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1140,14 +1140,21 @@ We may define as many different fetch profiles as we like.
| `@FetchProfileOverride` | Specifies the fetch strategy for the annotated association, in a given fetch profile
|===

A fetch profile must be explicitly enabled for a given session by calling link:{doc-javadoc-url}org/hibernate/Session.html#enableFetchProfile(java.lang.String)[`enableFetchProfile()`]:
A fetch profile must be explicitly enabled for a given session by passing the name of the profile to link:{doc-javadoc-url}org/hibernate/Session.html#enableFetchProfile(java.lang.String)[`enableFetchProfile()`]:

[source,java]
----
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`:

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

So why or when might we prefer named fetch profiles to entity graphs?
Well, it's really hard to say.
It's nice that this feature _exists_, and if you love it, that's great.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,41 @@
/**
* 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()}.
* <p>
* For example, this class defines a fetch profile:
* <pre>
* &#064;Entity
* &#064;FetchProfile(name = "WithAuthors")
* class Book {
* ...
*
* @param profileName the {@link org.hibernate.annotations.FetchProfile#name()}
* &#064;ManyToMany
* &#064;FetchProfileOverride(profile = Book_.PROFILE_WITH_AUTHORS)
* Set&lt;Author&gt; authors;
* }
* </pre>
* The fetch profile may be requested like this:
* <pre>
* Book bookWithAuthors =
* session.find(Book.class, isbn, Book_._WithAuthors)
* </pre>
* <p>
* When the static metamodel is not used, an {@code EnabledFetchProfile}
* may be instantiated directly, passing the name of the fetch profile
* as a string.
* <pre>
* Book bookWithAuthors =
* session.find(Book.class, isbn,
* new EnabledFetchProfile("WithAuthors"))
* </pre>
*
* @param profileName the {@linkplain org.hibernate.annotations.FetchProfile#name profile name}
*
* @since 7.0
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
FetchType fetch() default EAGER;

/**
* The name of the {@link FetchProfile fetch profile} in
* which this fetch mode should be applied.
* The name of the {@linkplain FetchProfile fetch profile}
* in which this fetch mode should be applied.
*/
String profile();
}