From cd06e56ab239095d8c047ff09dd7f2fc56439cf7 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Mon, 8 Apr 2024 21:49:01 +0200 Subject: [PATCH] fix previous-page navigation with KeyedResultList It was returning the results in reverse order. This caused a failure in the Data TCK. Signed-off-by: Gavin King --- .../internal/AbstractSqmSelectionQuery.java | 2 +- .../query/sqm/internal/KeyedResult.java | 24 ++++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) 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 8142a0c71e..edfef8903a 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 @@ -172,7 +172,7 @@ abstract class AbstractSqmSelectionQuery extends AbstractSelectionQuery { .performList(this); return new KeyedResultList<>( - collectResults( results, page.getSize() ), + collectResults( results, page.getSize(), keyedPage.getKeyInterpretation() ), collectKeys( results, page.getSize() ), keyedPage, nextPage( keyedPage, results ), 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 120e8e974a..899ab90b58 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 @@ -6,6 +6,9 @@ */ package org.hibernate.query.sqm.internal; +import org.hibernate.AssertionFailure; +import org.hibernate.query.KeyedPage.KeyInterpretation; + import java.util.ArrayList; import java.util.List; @@ -32,11 +35,26 @@ class KeyedResult { return key; } - static List collectResults(List> executed, int pageSize) { + static List collectResults(List> executed, int pageSize, KeyInterpretation interpretation) { + //note: given list probably has one more result than needed final int size = executed.size(); final List resultList = new ArrayList<>( size ); - for (int i = 0; i < size && i < pageSize; i++) { - resultList.add( executed.get(i).getResult() ); + switch ( interpretation ) { + case NO_KEY: + case KEY_OF_LAST_ON_PREVIOUS_PAGE: + for (int i = 0; i < size && i < pageSize; i++) { + resultList.add( executed.get(i).getResult() ); + } + break; + case KEY_OF_FIRST_ON_NEXT_PAGE: + for (int i = pageSize-1; i>=0; i--) { + if (i < size) { + resultList.add( executed.get(i).getResult() ); + } + } + break; + default: + throw new AssertionFailure("Unrecognized KeyInterpretation"); } return resultList; }