Skip to content

Commit 514df55

Browse files
committed
HHH-19096 Add a setNamedEntityGraph shortcut to the selection query
1 parent 1907e7e commit 514df55

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

Diff for: hibernate-core/src/main/java/org/hibernate/query/SelectionQuery.java

+22
Original file line numberDiff line numberDiff line change
@@ -293,13 +293,35 @@ default Stream<R> stream() {
293293
*/
294294
SelectionQuery<R> setEntityGraph(EntityGraph<? super R> graph, GraphSemantic semantic);
295295

296+
/**
297+
* Apply a {@linkplain jakarta.persistence.NamedEntityGraph named entity graph}
298+
* to the query.
299+
* <p>
300+
* May throw an {@link IllegalArgumentException} exception if no entity graph for a given name is found.
301+
*
302+
* @param graphName The name of the predefined named entity graph to apply
303+
* @param semantic The way an entity graph should be applied
304+
*
305+
* @since 7.0
306+
*/
307+
@Incubating
308+
default SelectionQuery<R> setNamedEntityGraph(String graphName, GraphSemantic semantic) {
309+
EntityGraph<? super R> namedEntityGraph = getNamedEntityGraph( graphName );
310+
if ( namedEntityGraph == null ) {
311+
throw new IllegalArgumentException( "No entity graph found for name " + graphName );
312+
}
313+
return setEntityGraph( namedEntityGraph, semantic );
314+
}
315+
296316
/**
297317
* Obtain an immutable reference to a predefined
298318
* {@linkplain jakarta.persistence.NamedEntityGraph named entity graph}
299319
* or return {@code null} if there is no predefined graph with the given
300320
* name.
301321
*
302322
* @param graphName The name of the predefined named entity graph
323+
*
324+
* @since 7.0
303325
*/
304326
@Incubating
305327
EntityGraph<? super R> getNamedEntityGraph(String graphName);

Diff for: hibernate-core/src/test/java/org/hibernate/orm/test/annotations/fetchprofile/NewGraphTest.java

+16-16
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import org.hibernate.annotations.NaturalId;
99
import org.hibernate.graph.GraphSemantic;
1010
import org.hibernate.graph.RootGraph;
11-
import org.hibernate.query.SelectionQuery;
1211
import org.hibernate.testing.orm.junit.DomainModel;
1312
import org.hibernate.testing.orm.junit.SessionFactory;
1413
import org.hibernate.testing.orm.junit.SessionFactoryScope;
@@ -19,6 +18,7 @@
1918
import java.util.Set;
2019

2120
import static jakarta.persistence.FetchType.LAZY;
21+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2222
import static org.hibernate.Hibernate.isInitialized;
2323
import static org.junit.jupiter.api.Assertions.assertFalse;
2424
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -173,7 +173,7 @@ public class NewGraphTest {
173173
}
174174

175175
@Test
176-
void subTypeEntityGraph(SessionFactoryScope scope) {
176+
void superTypeEntityGraph(SessionFactoryScope scope) {
177177
scope.inTransaction( s -> {
178178
A a = new A();
179179
Aa aa = new Aa();
@@ -205,27 +205,29 @@ void subTypeEntityGraph(SessionFactoryScope scope) {
205205
.getSingleResult() );
206206
assertFalse( isInitialized( a.bs ) );
207207

208-
List<Aa> as = scope.fromSession( s -> {
209-
SelectionQuery<Aa> query = s.createSelectionQuery( "from Aa", Aa.class );
210-
return query
211-
.setEntityGraph( query.getNamedEntityGraph( "a-graph" ), GraphSemantic.FETCH )
212-
.getResultList();
213-
} );
208+
List<Aa> as = scope.fromSession( s -> s.createSelectionQuery( "from Aa", Aa.class )
209+
.setNamedEntityGraph( "a-graph", GraphSemantic.FETCH )
210+
.getResultList() );
214211
for ( Aa el : as ) {
215212
assertTrue( isInitialized( el.bs ) );
216213
assertFalse( isInitialized( el.bss ) );
217214
}
218215

219-
as = scope.fromSession( s -> {
220-
SelectionQuery<Aa> query = s.createSelectionQuery( "from Aa", Aa.class );
221-
return query
222-
.setEntityGraph( query.getNamedEntityGraph( "aa-graph" ), GraphSemantic.FETCH )
223-
.getResultList();
224-
} );
216+
as = scope.fromSession( s -> s.createSelectionQuery( "from Aa", Aa.class )
217+
.setNamedEntityGraph( "aa-graph", GraphSemantic.FETCH )
218+
.getResultList() );
225219
for ( Aa el : as ) {
226220
assertTrue( isInitialized( el.bs ) );
227221
assertTrue( isInitialized( el.bss ) );
228222
}
223+
224+
scope.inSession( s -> {
225+
assertThatThrownBy( () -> s.createSelectionQuery( "from Aa", Aa.class )
226+
.setNamedEntityGraph( "i-do-not-exist-graph", GraphSemantic.FETCH )
227+
.getResultList() )
228+
.isInstanceOf( IllegalArgumentException.class );
229+
} );
230+
229231
}
230232

231233
@Entity(name = "E")
@@ -273,8 +275,6 @@ static class A {
273275
@NamedEntityGraph(name = "aa-graph", attributeNodes = {@NamedAttributeNode("bs"), @NamedAttributeNode("bss"),})
274276
@Entity(name = "Aa")
275277
static class Aa extends A {
276-
@Id @GeneratedValue
277-
Long id;
278278

279279
@OneToMany(mappedBy = "aa")
280280
Set<B> bss;

0 commit comments

Comments
 (0)