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