diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/QueryParameters.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/QueryParameters.java index 2ec488833a..ee13f461bc 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/QueryParameters.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/QueryParameters.java @@ -652,6 +652,7 @@ public final class QueryParameters { copy.processedSQL = this.processedSQL; copy.processedPositionalParameterTypes = this.processedPositionalParameterTypes; copy.processedPositionalParameterValues = this.processedPositionalParameterValues; + copy.passDistinctThrough = this.passDistinctThrough; return copy; } diff --git a/hibernate-core/src/test/java/org/hibernate/test/distinct/SelectDistinctHqlTest.java b/hibernate-core/src/test/java/org/hibernate/test/distinct/SelectDistinctHqlTest.java index 9706d719cd..25a64f4d18 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/distinct/SelectDistinctHqlTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/distinct/SelectDistinctHqlTest.java @@ -49,8 +49,7 @@ public class SelectDistinctHqlTest extends BaseNonConfigCoreFunctionalTestCase { }; } - @Test - public void test() { + protected void prepareTest() { doInHibernate( this::sessionFactory, session -> { Person person = new Person(); person.id = 1L; @@ -59,6 +58,15 @@ public class SelectDistinctHqlTest extends BaseNonConfigCoreFunctionalTestCase { person.addPhone( new Phone( "027-123-4567" ) ); person.addPhone( new Phone( "028-234-9876" ) ); } ); + } + + @Override + protected boolean isCleanupTestDataRequired() { + return true; + } + + @Test + public void test() { doInHibernate( this::sessionFactory, session -> { sqlStatementInterceptor.getSqlQueries().clear(); @@ -95,17 +103,38 @@ public class SelectDistinctHqlTest extends BaseNonConfigCoreFunctionalTestCase { String sqlQuery = sqlStatementInterceptor.getSqlQueries().getLast(); assertTrue( sqlQuery.contains( " distinct " ) ); } ); + } + @Test + @TestForIssue( jiraKey = "HHH-11726" ) + public void testDistinctPassThroughFalse() { doInHibernate( this::sessionFactory, session -> { sqlStatementInterceptor.getSqlQueries().clear(); List persons = session.createQuery( - "select distinct p from Person p left join fetch p.phones " ) - .setHint( QueryHints.HINT_PASS_DISTINCT_THROUGH, false ) - .getResultList(); + "select distinct p from Person p left join fetch p.phones ") + .setHint(QueryHints.HINT_PASS_DISTINCT_THROUGH, false) + .setMaxResults(5) + .getResultList(); assertEquals(1, persons.size()); String sqlQuery = sqlStatementInterceptor.getSqlQueries().getLast(); - assertFalse( sqlQuery.contains( " distinct " ) ); - } ); + assertFalse(sqlQuery.contains(" distinct ")); + }); + } + + @Test + @TestForIssue( jiraKey = "HHH-11726" ) + public void testDistinctPassThroughTrue() { + doInHibernate( this::sessionFactory, session -> { + sqlStatementInterceptor.getSqlQueries().clear(); + List persons = session.createQuery( + "select distinct p from Person p left join fetch p.phones ") + .setHint(QueryHints.HINT_PASS_DISTINCT_THROUGH, true) + .setMaxResults(5) + .getResultList(); + assertEquals(1, persons.size()); + String sqlQuery = sqlStatementInterceptor.getSqlQueries().getLast(); + assertTrue(sqlQuery.contains(" distinct ")); + }); } @Entity(name = "Person")