Skip to content

Commit 82a33c8

Browse files
mbelladebeikov
authored andcommitted
HHH-19905 Add test for issue
1 parent db8fe52 commit 82a33c8

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.orm.test.query.hql;
8+
9+
import jakarta.persistence.Entity;
10+
import jakarta.persistence.Id;
11+
import jakarta.persistence.ManyToOne;
12+
import org.hibernate.testing.orm.junit.DomainModel;
13+
import org.hibernate.testing.orm.junit.Jira;
14+
import org.hibernate.testing.orm.junit.SessionFactory;
15+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
16+
import org.junit.jupiter.api.AfterAll;
17+
import org.junit.jupiter.api.BeforeAll;
18+
import org.junit.jupiter.api.Test;
19+
20+
import static org.assertj.core.api.Assertions.assertThat;
21+
22+
/**
23+
* @author Christian Beikov
24+
*/
25+
@DomainModel(annotatedClasses = {
26+
ImplicitNestedJoinTest.RootEntity.class,
27+
ImplicitNestedJoinTest.FirstLevelReferencedEntity.class,
28+
ImplicitNestedJoinTest.SecondLevelReferencedEntityA.class,
29+
ImplicitNestedJoinTest.SecondLevelReferencedEntityB.class
30+
})
31+
@SessionFactory
32+
@Jira("https://hibernate.atlassian.net/browse/HHH-19905")
33+
public class ImplicitNestedJoinTest {
34+
35+
@Test
36+
public void testInnerAndLeftJoin(SessionFactoryScope scope) {
37+
scope.inSession( session -> {
38+
final var resultList = session.createQuery(
39+
"select r.id from RootEntity r"
40+
+ " join r.firstLevelReference.secondLevelReferenceA sa "
41+
+ " left join r.firstLevelReference.secondLevelReferenceB sb",
42+
Long.class
43+
).getResultList();
44+
assertThat( resultList ).hasSize( 2 ).containsExactlyInAnyOrder( 1L, 2L );
45+
} );
46+
}
47+
48+
@Test
49+
public void testLeftAndInnerJoin(SessionFactoryScope scope) {
50+
scope.inSession( session -> {
51+
final var resultList = session.createQuery(
52+
"select r.id from RootEntity r"
53+
+ " left join r.firstLevelReference.secondLevelReferenceA sa "
54+
+ " join r.firstLevelReference.secondLevelReferenceB sb",
55+
Long.class
56+
).getResultList();
57+
assertThat( resultList ).hasSize( 1 ).containsExactly( 1L );
58+
} );
59+
}
60+
61+
@Test
62+
public void testBothInnerJoins(SessionFactoryScope scope) {
63+
scope.inSession( session -> {
64+
final var resultList = session.createQuery(
65+
"select r.id from RootEntity r"
66+
+ " join r.firstLevelReference.secondLevelReferenceA sa "
67+
+ " join r.firstLevelReference.secondLevelReferenceB sb",
68+
Long.class
69+
).getResultList();
70+
assertThat( resultList ).hasSize( 1 ).containsExactly( 1L );
71+
} );
72+
}
73+
74+
@BeforeAll
75+
public void setUp(SessionFactoryScope scope) {
76+
scope.inTransaction( session -> {
77+
// create some test data : one first level reference with both second level references, and
78+
// another first level reference with only one second level reference
79+
SecondLevelReferencedEntityA secondLevelA = new SecondLevelReferencedEntityA();
80+
secondLevelA.id = 1L;
81+
secondLevelA.name = "Second Level A";
82+
session.persist( secondLevelA );
83+
SecondLevelReferencedEntityB secondLevelB = new SecondLevelReferencedEntityB();
84+
secondLevelB.id = 1L;
85+
session.persist( secondLevelB );
86+
FirstLevelReferencedEntity firstLevel1 = new FirstLevelReferencedEntity();
87+
firstLevel1.id = 1L;
88+
firstLevel1.secondLevelReferenceA = secondLevelA;
89+
firstLevel1.secondLevelReferenceB = secondLevelB;
90+
session.persist( firstLevel1 );
91+
RootEntity root1 = new RootEntity();
92+
root1.id = 1L;
93+
root1.firstLevelReference = firstLevel1;
94+
session.persist( root1 );
95+
FirstLevelReferencedEntity firstLevel2 = new FirstLevelReferencedEntity();
96+
firstLevel2.id = 2L;
97+
firstLevel2.secondLevelReferenceA = secondLevelA;
98+
session.persist( firstLevel2 );
99+
RootEntity root2 = new RootEntity();
100+
root2.id = 2L;
101+
root2.firstLevelReference = firstLevel2;
102+
session.persist( root2 );
103+
} );
104+
}
105+
106+
@AfterAll
107+
public void tearDown(SessionFactoryScope scope) {
108+
scope.getSessionFactory().getSchemaManager().truncateMappedObjects();
109+
}
110+
111+
@Entity(name = "RootEntity")
112+
public static class RootEntity {
113+
@Id
114+
private Long id;
115+
116+
@ManyToOne
117+
private FirstLevelReferencedEntity firstLevelReference;
118+
}
119+
120+
@Entity(name = "FirstLevelReferencedEntity")
121+
public static class FirstLevelReferencedEntity {
122+
@Id
123+
private Long id;
124+
@ManyToOne
125+
private SecondLevelReferencedEntityA secondLevelReferenceA;
126+
@ManyToOne
127+
private SecondLevelReferencedEntityB secondLevelReferenceB;
128+
129+
}
130+
131+
@Entity(name = "SecondLevelReferencedEntityA")
132+
public static class SecondLevelReferencedEntityA {
133+
@Id
134+
private Long id;
135+
private String name;
136+
}
137+
138+
@Entity(name = "SecondLevelReferencedEntityB")
139+
public static class SecondLevelReferencedEntityB {
140+
@Id
141+
private Long id;
142+
}
143+
}

0 commit comments

Comments
 (0)