Skip to content

Commit de93018

Browse files
committed
Retain scroll direction in Keyset position function of the correct item.
Closes #2999
1 parent be58b75 commit de93018

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/ScrollDelegate.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,14 @@ private static <T> Window<T> createWindow(Sort sort, int limit, Direction direct
8080
JpaEntityInformation<T, ?> entity, List<T> result) {
8181

8282
KeysetScrollDelegate delegate = KeysetScrollDelegate.of(direction);
83-
List<T> resultsToUse = delegate.postProcessResults(result);
83+
List<T> resultsToUse = delegate.getResultWindow(delegate.postProcessResults(result), limit);
8484

8585
IntFunction<ScrollPosition> positionFunction = value -> {
8686

87-
T object = result.get(value);
87+
T object = resultsToUse.get(value);
8888
Map<String, Object> keys = entity.getKeyset(sort.stream().map(Order::getProperty).toList(), object);
8989

90-
return ScrollPosition.forward(keys);
90+
return ScrollPosition.of(keys, direction);
9191
};
9292

9393
return Window.from(delegate.getResultWindow(resultsToUse, limit), positionFunction, hasMoreElements(result, limit));

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java

+26
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import org.springframework.data.domain.*;
5858
import org.springframework.data.domain.Sort.Direction;
5959
import org.springframework.data.domain.Sort.Order;
60+
import org.springframework.data.domain.ExampleMatcher.*;
6061
import org.springframework.data.jpa.domain.Specification;
6162
import org.springframework.data.jpa.domain.sample.Address;
6263
import org.springframework.data.jpa.domain.sample.QUser;
@@ -1304,6 +1305,31 @@ void scrollByExampleKeysetBackward() {
13041305
assertThat(previousWindow.hasNext()).isTrue();
13051306
}
13061307

1308+
@Test // GH-2999
1309+
void scrollInitiallyByExampleKeysetBackward() {
1310+
1311+
User jane1 = new User("Jane", "Doe", "[email protected]");
1312+
User jane2 = new User("Jane", "Doe", "[email protected]");
1313+
User john1 = new User("John", "Doe", "[email protected]");
1314+
User john2 = new User("John", "Doe", "[email protected]");
1315+
1316+
repository.saveAllAndFlush(Arrays.asList(john1, john2, jane1, jane2));
1317+
1318+
Example<User> example = Example.of(new User("J", null, null),
1319+
matching().withMatcher("firstname", GenericPropertyMatcher::startsWith).withIgnorePaths("age", "createdAt",
1320+
"dateOfBirth"));
1321+
1322+
Window<User> firstWindow = repository.findBy(example,
1323+
q -> q.limit(2).sortBy(Sort.by("firstname", "emailAddress")).scroll(ScrollPosition.keyset().backward()));
1324+
1325+
assertThat(firstWindow).containsExactly(john1, john2);
1326+
1327+
Window<User> previousWindow = repository.findBy(example,
1328+
q -> q.limit(2).sortBy(Sort.by("firstname", "emailAddress")).scroll(firstWindow.positionAt(0)));
1329+
1330+
assertThat(previousWindow).containsExactly(jane1, jane2);
1331+
}
1332+
13071333
@Test // GH-2878
13081334
void scrollByPredicateOffset() {
13091335

0 commit comments

Comments
 (0)