Skip to content

Commit 8a17c1e

Browse files
cigalymbellade
authored andcommitted
HHH-19396 Test case - selecting same column (under different alias) twice in same (sub)query
1 parent b84e651 commit 8a17c1e

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
8+
package org.hibernate.orm.test.subquery;
9+
10+
import jakarta.persistence.Entity;
11+
import jakarta.persistence.GeneratedValue;
12+
import jakarta.persistence.Id;
13+
import jakarta.persistence.Tuple;
14+
import org.hibernate.testing.orm.junit.DomainModel;
15+
import org.hibernate.testing.orm.junit.JiraKey;
16+
import org.hibernate.testing.orm.junit.SessionFactory;
17+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
18+
import org.junit.jupiter.api.AfterEach;
19+
import org.junit.jupiter.api.BeforeEach;
20+
import org.junit.jupiter.api.DisplayName;
21+
import org.junit.jupiter.api.Test;
22+
23+
import static org.junit.jupiter.api.Assertions.assertEquals;
24+
25+
@DomainModel(annotatedClasses = MultipleIdenticalColumnsInSubqueryTest.Something.class)
26+
@SessionFactory
27+
@JiraKey("HHH-19396")
28+
class MultipleIdenticalColumnsInSubqueryTest {
29+
30+
@BeforeEach
31+
void init(SessionFactoryScope scope) {
32+
scope.inTransaction( session -> session.persist( new Something() ) );
33+
}
34+
35+
@AfterEach
36+
void clean(SessionFactoryScope scope) {
37+
scope.inTransaction( session -> session.createMutationQuery( "delete from Something" ).executeUpdate() );
38+
}
39+
40+
@Test
41+
@DisplayName("Temporary table with same column selected twice, deduplication should be turned off")
42+
void CTE_with_same_column_selected_twice(SessionFactoryScope scope) {
43+
var r = scope.fromSession( session ->
44+
session.createSelectionQuery(
45+
"WITH S0 AS (SELECT foo AS foo, foo AS bar FROM Something) SELECT foo AS foo FROM S0",
46+
String.class ).getSingleResult() );
47+
assertEquals( "a", r );
48+
}
49+
50+
@Test
51+
@DisplayName("Subquery with same column selected twice, deduplication should be turned off")
52+
void CTE_with_same_column_selected_twice_some_aliases_removed(SessionFactoryScope scope) {
53+
var r = scope.fromSession( session ->
54+
session.createSelectionQuery(
55+
"SELECT foo AS foo FROM (SELECT foo AS foo, foo AS foo2 FROM Something)",
56+
String.class ).getSingleResult() );
57+
assertEquals( "a", r );
58+
}
59+
60+
@Test
61+
@DisplayName("Simple query with same column selected twice, deduplication should be turned on")
62+
void simple_query_with_same_column_selected_twice(SessionFactoryScope scope) {
63+
var tuple = scope.fromSession( session ->
64+
session.createSelectionQuery(
65+
"SELECT foo AS foo, foo as bar FROM Something",
66+
Tuple.class ).getSingleResult() );
67+
assertEquals( 2, tuple.getElements().size() );
68+
assertEquals( "a", tuple.get( "foo" ) );
69+
assertEquals( "a", tuple.get( "bar" ) );
70+
}
71+
72+
@Entity(name = "Something")
73+
static class Something {
74+
@Id
75+
@GeneratedValue
76+
private Long id;
77+
private String foo = "a";
78+
}
79+
}

0 commit comments

Comments
 (0)