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