77import java .util .HashSet ;
88import java .util .Set ;
99import jakarta .persistence .Entity ;
10- import jakarta .persistence .GeneratedValue ;
10+ import jakarta .persistence .FetchType ;
1111import jakarta .persistence .Id ;
1212import jakarta .persistence .JoinColumn ;
1313import jakarta .persistence .ManyToOne ;
1414import jakarta .persistence .NamedAttributeNode ;
1515import jakarta .persistence .NamedEntityGraph ;
1616import jakarta .persistence .Table ;
17- import jakarta .persistence .TypedQuery ;
1817
18+ import org .hibernate .jpa .SpecHints ;
1919import org .hibernate .testing .orm .junit .JiraKey ;
2020import org .hibernate .testing .orm .junit .EntityManagerFactoryScope ;
2121import org .hibernate .testing .orm .junit .Jpa ;
2222
23+ import org .junit .jupiter .api .BeforeAll ;
2324import org .junit .jupiter .api .Test ;
2425
26+ import static org .hibernate .Hibernate .isInitialized ;
27+ import static org .junit .jupiter .api .Assertions .assertTrue ;
28+
2529/**
2630 * Based on the test developed by Hans Desmet to reproduce the bug reported in HHH-9230
2731 *
3337})
3438public class QueryWithInParamListAndNamedEntityGraphTest {
3539
40+ @ BeforeAll
41+ void prepareTest (EntityManagerFactoryScope scope ) {
42+ scope .inTransaction (
43+ entityManager -> {
44+ Person boss = new Person ();
45+ boss .id = 2L ;
46+ boss .name = "B" ;
47+ entityManager .persist ( boss );
48+ Person person = new Person ();
49+ person .id = 1L ;
50+ person .name = "X" ;
51+ person .boss = boss ;
52+ entityManager .persist ( person );
53+ }
54+ );
55+ }
56+
3657 @ Test
3758 public void testInClause (EntityManagerFactoryScope scope ) {
3859 // this test works
@@ -41,9 +62,9 @@ public void testInClause(EntityManagerFactoryScope scope) {
4162 Set <Long > ids = new HashSet <>();
4263 ids .add ( 1L );
4364 ids .add ( 2L );
44- TypedQuery < Person > query = entityManager .createQuery ( "select p from Person p where p.id in :ids" , Person .class );
45- query .setParameter ( "ids" , ids );
46- query .getResultList ();
65+ entityManager .createQuery ( "select p from Person p where p.id in :ids" , Person .class )
66+ .setParameter ( "ids" , ids )
67+ .getResultList ();
4768 }
4869 );
4970 }
@@ -53,9 +74,10 @@ public void testEntityGraph(EntityManagerFactoryScope scope) {
5374 // this test works
5475 scope .inTransaction (
5576 entityManager -> {
56- TypedQuery <Person > query = entityManager .createQuery ( "select p from Person p" , Person .class );
57- query .setHint ( "javax.persistence.loadgraph" , entityManager .createEntityGraph ( "withBoss" ) );
58- query .getResultList ();
77+ entityManager .createQuery ( "select p from Person p" , Person .class )
78+ .setHint ( SpecHints .HINT_SPEC_LOAD_GRAPH ,
79+ entityManager .createEntityGraph ( QueryWithInParamListAndNamedEntityGraphTest_ .Person_ .GRAPH_WITH_BOSS ) )
80+ .getResultList ();
5981 }
6082 );
6183 }
@@ -68,23 +90,38 @@ public void testEntityGraphAndInClause(EntityManagerFactoryScope scope) {
6890 Set <Long > ids = new HashSet <>();
6991 ids .add ( 1L );
7092 ids .add ( 2L );
71- TypedQuery <Person > query = entityManager .createQuery ( "select p from Person p where p.id in :ids" , Person .class );
72- query .setHint ( "javax.persistence.loadgraph" , entityManager .createEntityGraph ( "withBoss" ) );
73- query .setParameter ( "ids" , ids );
74- query .getResultList ();
93+ entityManager .createQuery ( "select p from Person p where p.id in :ids" , Person .class )
94+ .setHint ( SpecHints .HINT_SPEC_LOAD_GRAPH ,
95+ entityManager .createEntityGraph ( QueryWithInParamListAndNamedEntityGraphTest_ .Person_ .GRAPH_WITH_BOSS ) )
96+ .setParameter ( "ids" , ids )
97+ .getResultList ();
98+ }
99+ );
100+ }
101+
102+ @ Test
103+ public void testNamedEntityGraph (EntityManagerFactoryScope scope ) {
104+ scope .inTransaction (
105+ entityManager -> {
106+ Person person =
107+ entityManager .createQuery ( "select p from Person p where p.boss is not null" , Person .class )
108+ .setHint ( SpecHints .HINT_SPEC_LOAD_GRAPH ,
109+ QueryWithInParamListAndNamedEntityGraphTest_ .Person_ .GRAPH_WITH_BOSS )
110+ .getSingleResult ();
111+ assertTrue ( isInitialized ( person .boss ) );
75112 }
76113 );
77114 }
78115
79116 @ Entity (name = "Person" )
80117 @ Table (name = "Person" )
81- @ NamedEntityGraph (name = "withBoss" , attributeNodes = @ NamedAttributeNode ("boss" ))
118+ @ NamedEntityGraph (name = "withBoss" ,
119+ attributeNodes = @ NamedAttributeNode ("boss" ))
82120 public static class Person {
83121 @ Id
84- @ GeneratedValue
85122 private long id ;
86123 private String name ;
87- @ ManyToOne
124+ @ ManyToOne ( fetch = FetchType . LAZY )
88125 @ JoinColumn
89126 private Person boss ;
90127 }
0 commit comments