From 706686114783166a0016f52cc720271b57fffc4c Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Fri, 31 May 2024 10:58:31 +0200 Subject: [PATCH] HHH-18202 Add test for issue --- .../query/ManyToManyGroupByOrderByTest.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/query/ManyToManyGroupByOrderByTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/query/ManyToManyGroupByOrderByTest.java index 3ce4623b6c..674371ddbf 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/query/ManyToManyGroupByOrderByTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/query/ManyToManyGroupByOrderByTest.java @@ -35,6 +35,7 @@ import static org.assertj.core.api.Assertions.assertThat; } ) @SessionFactory @Jira( "https://hibernate.atlassian.net/browse/HHH-17837" ) +@Jira( "https://hibernate.atlassian.net/browse/HHH-18202" ) public class ManyToManyGroupByOrderByTest { @Test public void testSelectEntity(SessionFactoryScope scope) { @@ -110,6 +111,42 @@ public class ManyToManyGroupByOrderByTest { } ); } + @Test + public void testDistinctAndAggregates(SessionFactoryScope scope) { + // explicit join distinct + scope.inTransaction( session -> { + final Tuple result = session.createQuery( + "select distinct owner.id from Cat cat join cat.owners owner group by owner.id order by owner.id", + Tuple.class + ).getSingleResult(); + assertThat( result.get( 0, Long.class ) ).isEqualTo( 1L ); + } ); + // explicit join distinct + aggregate + scope.inTransaction( session -> { + final Tuple result = session.createQuery( + "select distinct min(owner.id), cat.id from Cat cat join cat.owners owner group by cat.id order by min(owner.id), cat.id", + Tuple.class + ).getSingleResult(); + assertThat( result.get( 0, Long.class ) ).isEqualTo( 1L ); + } ); + // implicit join distinct + scope.inTransaction( session -> { + final Tuple result = session.createQuery( + "select distinct element(cat.owners).id from Cat cat group by element(cat.owners).id order by element(cat.owners).id", + Tuple.class + ).getSingleResult(); + assertThat( result.get( 0, Long.class ) ).isEqualTo( 1L ); + } ); + // implicit join distinct + aggregate + scope.inTransaction( session -> { + final Tuple result = session.createQuery( + "select distinct min(element(cat.owners).id), cat.id from Cat cat group by cat.id order by min(element(cat.owners).id), cat.id", + Tuple.class + ).getSingleResult(); + assertThat( result.get( 0, Long.class ) ).isEqualTo( 1L ); + } ); + } + @BeforeAll public void setUp(SessionFactoryScope scope) { scope.inTransaction( session -> {