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 a2d58b10f9..97a80e1438 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 @@ -181,29 +181,33 @@ abstract class AbstractSqmSelectionQuery extends AbstractSelectionQuery { } private static KeyedPage nextPage(KeyedPage keyedPage, List> results) { - if (keyedPage.getKeyInterpretation() == KEY_OF_FIRST_ON_NEXT_PAGE) { + if ( keyedPage.getKeyInterpretation() == KEY_OF_FIRST_ON_NEXT_PAGE ) { + // the results come in reverse order return !results.isEmpty() ? keyedPage.nextPage( results.get(0).getKey() ) : null; } - - final int pageSize = keyedPage.getPage().getSize(); - return results.size() == pageSize + 1 - ? keyedPage.nextPage( results.get(pageSize - 1).getKey() ) - : null; + else { + final int pageSize = keyedPage.getPage().getSize(); + return results.size() == pageSize + 1 + ? keyedPage.nextPage( results.get(pageSize - 1).getKey() ) + : null; + } } private static KeyedPage previousPage(KeyedPage keyedPage, List> results) { - if (keyedPage.getKeyInterpretation() == KEY_OF_FIRST_ON_NEXT_PAGE) { + if ( keyedPage.getKeyInterpretation() == KEY_OF_FIRST_ON_NEXT_PAGE ) { + // the results come in reverse order final int pageSize = keyedPage.getPage().getSize(); return results.size() == pageSize + 1 ? keyedPage.previousPage( results.get(pageSize - 1).getKey() ) : null; } - - return !results.isEmpty() - ? keyedPage.previousPage( results.get(0).getKey() ) - : null; + else { + return !results.isEmpty() + ? keyedPage.previousPage( results.get(0).getKey() ) + : null; + } } public abstract Class getExpectedResultType(); diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/KeyedResult.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/KeyedResult.java index 899ab90b58..bb72abb369 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/KeyedResult.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/KeyedResult.java @@ -47,6 +47,7 @@ class KeyedResult { } break; case KEY_OF_FIRST_ON_NEXT_PAGE: + // the results come in reverse order for (int i = pageSize-1; i>=0; i--) { if (i < size) { resultList.add( executed.get(i).getResult() );