Skip to content

Commit 823a2a8

Browse files
committed
HHH-19468 allow an EnabledFetchProfile to be apply()d to a Session or Query
1 parent fec167b commit 823a2a8

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

documentation/src/main/asciidoc/introduction/Advanced.adoc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1148,7 +1148,15 @@ session.enableFetchProfile(Book_.PROFILE_EAGER_BOOK);
11481148
Book eagerBook = session.find(Book.class, bookId);
11491149
----
11501150

1151-
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`:
1151+
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:
1152+
1153+
[source,java]
1154+
----
1155+
Book_._EagerBook.enable(session);
1156+
Book eagerBook = session.find(Book.class, bookId);
1157+
----
1158+
1159+
Even better, the `EnabledFetchProfile` may be passed as a `FindOption`:
11521160

11531161
[source,java]
11541162
----

hibernate-core/src/main/java/org/hibernate/EnabledFetchProfile.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@
55
package org.hibernate;
66

77
import jakarta.persistence.FindOption;
8+
import org.hibernate.query.SelectionQuery;
89

910
/**
1011
* A {@link jakarta.persistence.FindOption} which requests a named
1112
* {@linkplain org.hibernate.annotations.FetchProfile fetch profile}.
1213
* <p>
1314
* An instance of this class may be obtained in a type safe way
1415
* from the static metamodel for the class annotated
15-
* {@link org.hibernate.annotations.FetchProfile @FetchProfile}
16-
* and passed as an option to
17-
* {@link Session#find(Class, Object, FindOption...) find()}.
16+
* {@link org.hibernate.annotations.FetchProfile @FetchProfile}.
1817
* <p>
1918
* For example, this class defines a fetch profile:
2019
* <pre>
@@ -28,11 +27,20 @@
2827
* Set&lt;Author&gt; authors;
2928
* }
3029
* </pre>
31-
* The fetch profile may be requested like this:
30+
* <p>
31+
* An {@code EnabledFetchProfile} may be obtained from the static
32+
* metamodel for the entity {@code Book} and passed as an option to
33+
* {@link Session#find(Class, Object, FindOption...) find()}.
3234
* <pre>
3335
* Book bookWithAuthors =
3436
* session.find(Book.class, isbn, Book_._WithAuthors)
3537
* </pre>
38+
* Alternatively, it may be {@linkplain #enable(Session) applied}
39+
* to a {@code Session} or {@code Query}.
40+
* <pre>
41+
* Book_._WithAuthors.enable(session);
42+
* Book bookWithAuthors = session.find(Book.class, isbn);
43+
* </pre>
3644
* <p>
3745
* When the static metamodel is not used, an {@code EnabledFetchProfile}
3846
* may be instantiated directly, passing the name of the fetch profile
@@ -54,4 +62,20 @@
5462
*/
5563
public record EnabledFetchProfile(String profileName)
5664
implements FindOption {
65+
66+
/**
67+
* Enable the fetch profile represented by this
68+
* object in the given session.
69+
*/
70+
public void enable(Session session) {
71+
session.enableFetchProfile(profileName);
72+
}
73+
74+
/**
75+
* Enable the fetch profile represented by this
76+
* object during execution of the given query.
77+
*/
78+
public void enable(SelectionQuery<?> query) {
79+
query.enableFetchProfile(profileName);
80+
}
5781
}

0 commit comments

Comments
 (0)