Skip to content

Commit 3132f49

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

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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import org.springframework.data.domain.ExampleMatcher.StringMatcher;
6161
import org.springframework.data.domain.Sort.Direction;
6262
import org.springframework.data.domain.Sort.Order;
63+
import org.springframework.data.domain.ExampleMatcher.*;
6364
import org.springframework.data.jpa.domain.Specification;
6465
import org.springframework.data.jpa.domain.sample.Address;
6566
import org.springframework.data.jpa.domain.sample.QUser;
@@ -1308,6 +1309,31 @@ void scrollByExampleKeysetBackward() {
13081309
assertThat(previousWindow.hasNext()).isTrue();
13091310
}
13101311

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

0 commit comments

Comments
 (0)