|
5 | 5 | package org.hibernate;
|
6 | 6 |
|
7 | 7 | import jakarta.persistence.FindOption;
|
| 8 | +import org.hibernate.query.SelectionQuery; |
8 | 9 |
|
9 | 10 | /**
|
10 | 11 | * A {@link jakarta.persistence.FindOption} which requests a named
|
11 | 12 | * {@linkplain org.hibernate.annotations.FetchProfile fetch profile}.
|
12 | 13 | * <p>
|
13 | 14 | * An instance of this class may be obtained in a type safe way
|
14 | 15 | * 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}. |
18 | 17 | * <p>
|
19 | 18 | * For example, this class defines a fetch profile:
|
20 | 19 | * <pre>
|
|
28 | 27 | * Set<Author> authors;
|
29 | 28 | * }
|
30 | 29 | * </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()}. |
32 | 34 | * <pre>
|
33 | 35 | * Book bookWithAuthors =
|
34 | 36 | * session.find(Book.class, isbn, Book_._WithAuthors)
|
35 | 37 | * </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> |
36 | 44 | * <p>
|
37 | 45 | * When the static metamodel is not used, an {@code EnabledFetchProfile}
|
38 | 46 | * may be instantiated directly, passing the name of the fetch profile
|
|
54 | 62 | */
|
55 | 63 | public record EnabledFetchProfile(String profileName)
|
56 | 64 | 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 | + } |
57 | 81 | }
|
0 commit comments