From 8f217c886fb68250bc20e4879cf7be61a32eda68 Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Thu, 30 May 2024 12:05:54 +0200 Subject: [PATCH] HHH-18121 Add test for issue --- .../test/query/criteria/CountQueryTests.java | 59 ++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/query/criteria/CountQueryTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/query/criteria/CountQueryTests.java index 13b861ddef..5492458d61 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/query/criteria/CountQueryTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/query/criteria/CountQueryTests.java @@ -9,6 +9,7 @@ package org.hibernate.orm.test.query.criteria; import java.time.LocalDate; import java.util.List; +import org.hibernate.annotations.Imported; import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.query.criteria.HibernateCriteriaBuilder; import org.hibernate.query.criteria.JpaCriteriaQuery; @@ -20,6 +21,7 @@ import org.hibernate.testing.orm.domain.contacts.Address; import org.hibernate.testing.orm.domain.contacts.Contact; import org.hibernate.testing.orm.junit.DialectFeatureChecks; import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.Jira; import org.hibernate.testing.orm.junit.JiraKey; import org.hibernate.testing.orm.junit.RequiresDialectFeature; import org.hibernate.testing.orm.junit.SessionFactory; @@ -49,7 +51,11 @@ import static org.junit.jupiter.api.Assertions.fail; */ @DomainModel( standardModels = StandardDomainModel.CONTACTS, - annotatedClasses = {CountQueryTests.LogSupport.class, CountQueryTests.Contract.class} + annotatedClasses = { + CountQueryTests.LogSupport.class, + CountQueryTests.Contract.class, + CountQueryTests.SimpleDto.class, + } ) @SessionFactory public class CountQueryTests { @@ -191,6 +197,49 @@ public class CountQueryTests { ); } + @Test + @Jira( "https://hibernate.atlassian.net/browse/HHH-18121" ) + public void testDistinctDynamicInstantiation(SessionFactoryScope scope) { + scope.inTransaction( session -> { + final HibernateCriteriaBuilder cb = session.getCriteriaBuilder(); + final JpaCriteriaQuery cq = cb.createQuery( Tuple.class ); + final JpaRoot root = cq.from( Contact.class ); + cq.multiselect( + root.get( "name" ).get( "last" ), + cb.construct( + SimpleDto.class, + root.get( "name" ).get( "last" ) + ) + ).distinct( true ); + final Long count = session.createQuery( cq.createCountQuery() ).getSingleResult(); + final List resultList = session.createQuery( cq ).getResultList(); + assertEquals( 1L, count ); + assertEquals( resultList.size(), count.intValue() ); + } ); + } + + @Test + @Jira( "https://hibernate.atlassian.net/browse/HHH-18121" ) + public void testUnionQuery(SessionFactoryScope scope) { + scope.inTransaction( session -> { + final HibernateCriteriaBuilder cb = session.getCriteriaBuilder(); + + final JpaCriteriaQuery cq1 = cb.createQuery( String.class ); + final JpaRoot root1 = cq1.from( Contact.class ); + cq1.multiselect( root1.get( "name" ).get( "first" ) ).where( cb.equal( root1.get( "id" ), 1 ) ); + + final JpaCriteriaQuery cq2 = cb.createQuery( String.class ); + final JpaRoot root2 = cq2.from( Contact.class ); + cq2.select( root2.get( "name" ).get( "first" ) ).where( cb.equal( root2.get( "id" ), 2 ) ); + + final JpaCriteriaQuery union = cb.union( cq1, cq2 ); + final Long count = session.createQuery( union.createCountQuery() ).getSingleResult(); + final List resultList = session.createQuery( union ).getResultList(); + assertEquals( 2L, count ); + assertEquals( resultList.size(), count.intValue() ); + } ); + } + @BeforeEach public void prepareTestData(SessionFactoryScope scope) { scope.inTransaction( (session) -> { @@ -299,4 +348,12 @@ public class CountQueryTests { private String customerName; } + @Imported + public static class SimpleDto { + private String name; + + public SimpleDto(String name) { + this.name = name; + } + } }