From 673a4df2674443ea21bc5269eeb282d6f54acf22 Mon Sep 17 00:00:00 2001 From: Yanming Zhou Date: Tue, 6 Jun 2023 15:49:37 +0800 Subject: [PATCH] Fix positionFunction created by ScrollDelegate Before this commit, positionFunction created by ScrollDelegate retains incorrect state when perform backward scrolling. 1. We must use window.positionAt(1) instead of window.positionAt(0) if we want position at the begin of window, we should call getResultWindow() first. 2. The direction always be FORWARD, it should be BACKWARD if the original direction is BACKWARD. Closes GH-2999 --- .../data/jpa/repository/query/ScrollDelegate.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/ScrollDelegate.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/ScrollDelegate.java index 10681ac698..b3e7ad5d2f 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/ScrollDelegate.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/ScrollDelegate.java @@ -35,6 +35,7 @@ * Delegate to run {@link ScrollPosition scroll queries} and create result {@link Window}. * * @author Mark Paluch + * @author Yanming Zhou * @since 3.1 */ public class ScrollDelegate { @@ -80,17 +81,17 @@ private static Window createWindow(Sort sort, int limit, Direction direct JpaEntityInformation entity, List result) { KeysetScrollDelegate delegate = KeysetScrollDelegate.of(direction); - List resultsToUse = delegate.postProcessResults(result); + List resultsToUse = delegate.getResultWindow(delegate.postProcessResults(result), limit); IntFunction positionFunction = value -> { - T object = result.get(value); + T object = resultsToUse.get(value); Map keys = entity.getKeyset(sort.stream().map(Order::getProperty).toList(), object); - return ScrollPosition.forward(keys); + return ScrollPosition.of(keys, direction); }; - return Window.from(delegate.getResultWindow(resultsToUse, limit), positionFunction, hasMoreElements(result, limit)); + return Window.from(resultsToUse, positionFunction, hasMoreElements(result, limit)); } private static Window createWindow(List result, int limit,