Skip to content

Commit 72af4a4

Browse files
committed
HHH-19612 test EntityGraph name in the JPA standard hint
1 parent 3086df6 commit 72af4a4

File tree

1 file changed

+52
-15
lines changed

1 file changed

+52
-15
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/jpa/query/QueryWithInParamListAndNamedEntityGraphTest.java

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,25 @@
77
import java.util.HashSet;
88
import java.util.Set;
99
import jakarta.persistence.Entity;
10-
import jakarta.persistence.GeneratedValue;
10+
import jakarta.persistence.FetchType;
1111
import jakarta.persistence.Id;
1212
import jakarta.persistence.JoinColumn;
1313
import jakarta.persistence.ManyToOne;
1414
import jakarta.persistence.NamedAttributeNode;
1515
import jakarta.persistence.NamedEntityGraph;
1616
import jakarta.persistence.Table;
17-
import jakarta.persistence.TypedQuery;
1817

18+
import org.hibernate.jpa.SpecHints;
1919
import org.hibernate.testing.orm.junit.JiraKey;
2020
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
2121
import org.hibernate.testing.orm.junit.Jpa;
2222

23+
import org.junit.jupiter.api.BeforeAll;
2324
import 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
*
@@ -33,6 +37,23 @@
3337
})
3438
public 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

Comments
 (0)