Skip to content

Commit 8e27a8d

Browse files
committed
HHH-18720 Add test for issue
1 parent 1b00f69 commit 8e27a8d

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.query.hql;
6+
7+
import jakarta.persistence.Entity;
8+
import jakarta.persistence.Id;
9+
import jakarta.persistence.Tuple;
10+
import org.hibernate.testing.orm.junit.DomainModel;
11+
import org.hibernate.testing.orm.junit.JiraKey;
12+
import org.hibernate.testing.orm.junit.SessionFactory;
13+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
14+
import org.junit.jupiter.api.AfterEach;
15+
import org.junit.jupiter.api.BeforeEach;
16+
import org.junit.jupiter.api.Test;
17+
18+
import java.util.HashSet;
19+
import java.util.List;
20+
import java.util.Set;
21+
22+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
23+
24+
@DomainModel(
25+
annotatedClasses = {
26+
UnionAllSelectNullTest.TestEntity.class,
27+
UnionAllSelectNullTest.AnotherTestEntity.class
28+
}
29+
)
30+
@SessionFactory
31+
@JiraKey("HHH-18720")
32+
class UnionAllSelectNullTest {
33+
34+
@BeforeEach
35+
public void setUp(SessionFactoryScope scope) {
36+
scope.inTransaction(
37+
session -> {
38+
session.persist( new TestEntity( 1L, "a" ) );
39+
session.persist( new AnotherTestEntity( 2L, "b" ) );
40+
}
41+
);
42+
}
43+
44+
@AfterEach
45+
public void tearDown(SessionFactoryScope scope) {
46+
scope.inTransaction(
47+
session -> {
48+
session.createQuery( "delete TestEntity" ).executeUpdate();
49+
session.createQuery( "delete AnotherTestEntity" ).executeUpdate();
50+
}
51+
);
52+
}
53+
54+
@Test
55+
void testSelect(SessionFactoryScope scope) {
56+
scope.inTransaction(
57+
session -> {
58+
List<Tuple> resultList = session.createQuery(
59+
"SELECT te.id as id from TestEntity te"
60+
+ " union all SELECT null as id from AnotherTestEntity ate"
61+
, Tuple.class )
62+
.getResultList();
63+
assertThat( resultList.size() ).isEqualTo( 2 );
64+
assertResultIsCorrect( resultList, 1L, null );
65+
}
66+
);
67+
}
68+
69+
@Test
70+
void testSelect2(SessionFactoryScope scope) {
71+
scope.inTransaction(
72+
session -> {
73+
List<Tuple> resultList = session.createQuery(
74+
"SELECT null as id from TestEntity te"
75+
+ " union all SELECT ate.id as id from AnotherTestEntity ate"
76+
, Tuple.class )
77+
.getResultList();
78+
assertThat( resultList.size() ).isEqualTo( 2 );
79+
assertResultIsCorrect( resultList, null, 2L );
80+
}
81+
);
82+
}
83+
84+
@Test
85+
void testSelect3(SessionFactoryScope scope) {
86+
scope.inTransaction(
87+
session -> {
88+
List<Tuple> resultList = session.createQuery(
89+
"SELECT te.id as id from TestEntity te"
90+
+ " union all SELECT ate.id as id from AnotherTestEntity ate"
91+
, Tuple.class )
92+
.getResultList();
93+
assertThat( resultList.size() ).isEqualTo( 2 );
94+
assertResultIsCorrect( resultList, 1L, 2L );
95+
}
96+
);
97+
}
98+
99+
private static void assertResultIsCorrect(List<Tuple> resultList, Long id1, Long id2) {
100+
Set<Long> ids = new HashSet<>( 2 );
101+
ids.add( (Long) resultList.get( 0 ).get( "id" ) );
102+
ids.add( (Long) resultList.get( 1 ).get( "id" ) );
103+
assertThat( ids.contains( id1 ) ).as( "Result does not contain expected value:" + id1 ).isTrue();
104+
assertThat( ids.contains( id2 ) ).as( "Result does not contain expected value:" + id2 ).isTrue();
105+
}
106+
107+
@Entity(name = "TestEntity")
108+
public static class TestEntity {
109+
110+
@Id
111+
private Long id;
112+
113+
private String name;
114+
115+
public TestEntity() {
116+
}
117+
118+
public TestEntity(Long id, String name) {
119+
this.id = id;
120+
this.name = name;
121+
}
122+
}
123+
124+
@Entity(name = "AnotherTestEntity")
125+
public static class AnotherTestEntity {
126+
127+
@Id
128+
private Long id;
129+
130+
private String name;
131+
132+
public AnotherTestEntity() {
133+
}
134+
135+
public AnotherTestEntity(Long id, String name) {
136+
this.id = id;
137+
this.name = name;
138+
}
139+
}
140+
}

0 commit comments

Comments
 (0)