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 <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-04-08 21:49:01 +02:00 committed by Christian Beikov
parent 96855d0525
commit cd06e56ab2
2 changed files with 22 additions and 4 deletions

View File

@ -172,7 +172,7 @@ abstract class AbstractSqmSelectionQuery<R> extends AbstractSelectionQuery<R> {
.performList(this);
return new KeyedResultList<>(
collectResults( results, page.getSize() ),
collectResults( results, page.getSize(), keyedPage.getKeyInterpretation() ),
collectKeys( results, page.getSize() ),
keyedPage,
nextPage( keyedPage, results ),

View File

@ -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<R> {
return key;
}
static <R> List<R> collectResults(List<KeyedResult<R>> executed, int pageSize) {
static <R> List<R> collectResults(List<KeyedResult<R>> executed, int pageSize, KeyInterpretation interpretation) {
//note: given list probably has one more result than needed
final int size = executed.size();
final List<R> 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;
}