Skip to content

Commit 2601da5

Browse files
committed
HHH-19468 allow an EnabledFetchProfile to be apply()d to a Session or Query
1 parent af8c107 commit 2601da5

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1140,14 +1140,29 @@ We may define as many different fetch profiles as we like.
11401140
| `@FetchProfileOverride` | Specifies the fetch strategy for the annotated association, in a given fetch profile
11411141
|===
11421142

1143-
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()`]:
1143+
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()`]:
11441144

11451145
[source,java]
11461146
----
11471147
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 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+
An `EnabledFetchProfile` may even be used as a `FindOption`:
1160+
1161+
[source,java]
1162+
----
1163+
Book eagerBook = entityManager.find(Book.class, bookId, Book_._EagerBook);
1164+
----
1165+
11511166
So why or when might we prefer named fetch profiles to entity graphs?
11521167
Well, it's really hard to say.
11531168
It's nice that this feature _exists_, and if you love it, that's great.

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,28 @@
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}.
13+
* <p>
14+
* An instance of this class may be obtained in a type safe way
15+
* from the static metamodel for the class annotated
16+
* {@link org.hibernate.annotations.FetchProfile @FetchProfile}.
17+
* <p>
18+
* An {@code EnabledFetchProfile} may be passed as an option to
19+
* {@link Session#find(Class, Object, FindOption...) find()}.
20+
* <pre>
21+
* session.find(Book.class, isbn, Book_._WithAuthors)
22+
* </pre>
23+
* Alternatively, it may be {@linkplain #enable(Session) applied}
24+
* to a {@code Session} or {@code Query}.
25+
* <pre>
26+
* Book_._WithAuthors.enable(session)
27+
* </pre>
1228
*
13-
* @param profileName the {@link org.hibernate.annotations.FetchProfile#name()}
29+
* @param profileName the {@linkplain org.hibernate.annotations.FetchProfile#name profile name}
1430
*
1531
* @since 7.0
1632
*
@@ -21,4 +37,20 @@
2137
*/
2238
public record EnabledFetchProfile(String profileName)
2339
implements FindOption {
40+
41+
/**
42+
* Enable the fetch profile represented by this
43+
* object in the given session.
44+
*/
45+
public void enable(Session session) {
46+
session.enableFetchProfile(profileName);
47+
}
48+
49+
/**
50+
* Enable the fetch profile represented by this
51+
* object during execution of the given query.
52+
*/
53+
public void enable(SelectionQuery<?> query) {
54+
query.enableFetchProfile(profileName);
55+
}
2456
}

0 commit comments

Comments
 (0)