|
| 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.GenerationType; |
| 10 | +import jakarta.persistence.Id; |
| 11 | +import jakarta.persistence.Tuple; |
| 12 | +import jakarta.persistence.TupleElement; |
| 13 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 14 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 15 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 16 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 17 | +import org.junit.jupiter.api.AfterEach; |
| 18 | +import org.junit.jupiter.api.BeforeEach; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import static java.util.stream.Collectors.toSet; |
| 22 | +import static org.assertj.core.api.Assertions.assertThat; |
| 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 | + void CTE_with_same_column_selected_twice(SessionFactoryScope scope) { |
| 42 | + var r = scope.fromSession( session -> |
| 43 | + session.createSelectionQuery( |
| 44 | + "WITH S0 AS (SELECT foo AS foo2, foo AS foo FROM Something)" + |
| 45 | + "SELECT S0_0.foo AS foo FROM S0 AS S0_0", |
| 46 | + String.class ).getSingleResult() ); |
| 47 | + assertEquals( "a", r ); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void CTE_with_same_column_selected_twice_some_aliases_removed(SessionFactoryScope scope) { |
| 52 | + var r = scope.fromSession( session -> |
| 53 | + session.createSelectionQuery( |
| 54 | + "WITH S0 AS (SELECT foo AS foo, foo AS foo2 FROM Something)" + |
| 55 | + "SELECT foo AS foo FROM S0", |
| 56 | + String.class ).getSingleResult() ); |
| 57 | + assertEquals( "a", r ); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void simple_query_with_same_column_selected_twice(SessionFactoryScope scope) { |
| 62 | + var tuple = scope.fromSession( session -> |
| 63 | + session.createSelectionQuery( |
| 64 | + "SELECT foo AS foo, foo as foo2 FROM Something", |
| 65 | + Tuple.class ).getSingleResult() ); |
| 66 | + assertThat( tuple.getElements().stream().map( TupleElement::getAlias ).collect( toSet() ) ) |
| 67 | + .containsExactlyInAnyOrder( "foo", "foo2" ); |
| 68 | + } |
| 69 | + |
| 70 | + @Entity(name = "Something") |
| 71 | + static class Something { |
| 72 | + @Id |
| 73 | + @GeneratedValue(strategy = GenerationType.IDENTITY) |
| 74 | + private Long id; |
| 75 | + private String foo = "a"; |
| 76 | + } |
| 77 | +} |
0 commit comments