diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/AbstractSqmSelectionQuery.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/AbstractSqmSelectionQuery.java index 5f077cc3b0..e3a00d9034 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/AbstractSqmSelectionQuery.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/AbstractSqmSelectionQuery.java @@ -35,8 +35,6 @@ import org.hibernate.query.sqm.tree.predicate.SqmWhereClause; import org.hibernate.query.sqm.tree.select.SqmQuerySpec; import org.hibernate.query.sqm.tree.select.SqmSelectStatement; import org.hibernate.sql.results.internal.TupleMetadata; -import org.hibernate.sql.results.spi.ListResultsConsumer; -import org.hibernate.sql.results.spi.ListResultsConsumer.UniqueSemantic; import java.util.ArrayList; import java.util.List; @@ -238,15 +236,26 @@ abstract class AbstractSqmSelectionQuery extends AbstractSelectionQuery { keyed( keyDefinition, key ); } - final SqmSelectStatement> sqm = keyed( keyDefinition, key ); final ConcreteSqmSelectQueryPlan> plan = - buildConcreteQueryPlan( sqm, null, null, getQueryOptions() ); - final List> executed = - plan.executeQuery( this, new ListResultsConsumer<>(UniqueSemantic.NONE) ); - final List> keyOfLastResult = - executed.isEmpty() ? key : executed.get( executed.size()-1 ).getKey(); - return new KeyedResultList<>( executed.stream().map(KeyedResult::getResult).collect(toList()), - keyedPage, KeyedPage.forKey( keyDefinition, page.next(), keyOfLastResult ) ); + buildConcreteQueryPlan( keyed( keyDefinition, key ), + null, null, + getQueryOptions() ); + final List> executed = plan.performList(this); + final KeyedPage nextPage = + KeyedPage.forKey(keyDefinition, page.next(), + getKeyOfLastResult(executed, key)); + return new KeyedResultList<>( collectResultList( executed ), keyedPage, nextPage ); + } + + private static List collectResultList(List> executed) { + return executed.stream() + .map(KeyedResult::getResult) + .collect(toList()); + } + + private static List> getKeyOfLastResult( + List> executed, List> key) { + return executed.isEmpty() ? key : executed.get(executed.size() - 1).getKey(); } public abstract Class getExpectedResultType();