Skip to content

Commit 87bd578

Browse files
committed
Adapt to ScrollPosition API changes in Spring Data Commons.
Fixes #2931. Related ticket #2824.
1 parent 4d1c4ad commit 87bd578

File tree

4 files changed

+23
-24
lines changed

4 files changed

+23
-24
lines changed

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import java.util.Map;
2222

2323
import org.springframework.data.domain.KeysetScrollPosition;
24-
import org.springframework.data.domain.KeysetScrollPosition.Direction;
24+
import org.springframework.data.domain.ScrollPosition.Direction;
2525
import org.springframework.data.domain.Sort;
2626
import org.springframework.data.domain.Sort.Order;
2727
import org.springframework.lang.Nullable;
@@ -34,8 +34,8 @@
3434
*/
3535
public class KeysetScrollDelegate {
3636

37-
private static final KeysetScrollDelegate forward = new KeysetScrollDelegate();
38-
private static final KeysetScrollDelegate reverse = new ReverseKeysetScrollDelegate();
37+
private static final KeysetScrollDelegate FORWARD = new KeysetScrollDelegate();
38+
private static final KeysetScrollDelegate REVERSE = new ReverseKeysetScrollDelegate();
3939

4040
/**
4141
* Factory method to obtain the right {@link KeysetScrollDelegate}.
@@ -44,7 +44,7 @@ public class KeysetScrollDelegate {
4444
* @return a {@link KeysetScrollDelegate} matching the requested direction.
4545
*/
4646
public static KeysetScrollDelegate of(Direction direction) {
47-
return direction == Direction.Forward ? forward : reverse;
47+
return direction == Direction.FORWARD ? FORWARD : REVERSE;
4848
}
4949

5050
@Nullable
@@ -119,6 +119,7 @@ protected <T> List<T> getResultWindow(List<T> list, int limit) {
119119
*/
120120
private static class ReverseKeysetScrollDelegate extends KeysetScrollDelegate {
121121

122+
@Override
122123
protected Sort getSortOrders(Sort sort) {
123124

124125
List<Order> orders = new ArrayList<>();

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
import java.util.function.IntFunction;
2323

2424
import org.springframework.data.domain.KeysetScrollPosition;
25-
import org.springframework.data.domain.KeysetScrollPosition.Direction;
2625
import org.springframework.data.domain.OffsetScrollPosition;
2726
import org.springframework.data.domain.ScrollPosition;
27+
import org.springframework.data.domain.ScrollPosition.Direction;
2828
import org.springframework.data.domain.Sort;
2929
import org.springframework.data.domain.Sort.Order;
3030
import org.springframework.data.domain.Window;
@@ -82,12 +82,12 @@ private static <T> Window<T> createWindow(Sort sort, int limit, Direction direct
8282
KeysetScrollDelegate delegate = KeysetScrollDelegate.of(direction);
8383
List<T> resultsToUse = delegate.postProcessResults(result);
8484

85-
IntFunction<KeysetScrollPosition> positionFunction = value -> {
85+
IntFunction<ScrollPosition> positionFunction = value -> {
8686

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

90-
return KeysetScrollPosition.of(keys);
90+
return ScrollPosition.forward(keys);
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/RepositoryWithIdClassKeyTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.springframework.beans.factory.annotation.Autowired;
2626
import org.springframework.context.annotation.Configuration;
2727
import org.springframework.context.annotation.ImportResource;
28-
import org.springframework.data.domain.KeysetScrollPosition;
28+
import org.springframework.data.domain.ScrollPosition;
2929
import org.springframework.data.domain.Sort;
3030
import org.springframework.data.domain.Window;
3131
import org.springframework.data.jpa.domain.sample.Item;
@@ -90,7 +90,7 @@ void shouldScrollWithKeyset() {
9090

9191
Window<Item> first = itemRepository.findBy((root, query, criteriaBuilder) -> {
9292
return criteriaBuilder.isNotNull(root.get("name"));
93-
}, q -> q.limit(1).sortBy(Sort.by("name")).scroll(KeysetScrollPosition.initial()));
93+
}, q -> q.limit(1).sortBy(Sort.by("name")).scroll(ScrollPosition.keyset()));
9494

9595
assertThat(first).containsOnly(item1);
9696

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

+13-15
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
import org.springframework.dao.IncorrectResultSizeDataAccessException;
5757
import org.springframework.dao.InvalidDataAccessApiUsageException;
5858
import org.springframework.data.domain.*;
59+
import org.springframework.data.domain.ExampleMatcher.GenericPropertyMatcher;
60+
import org.springframework.data.domain.ExampleMatcher.StringMatcher;
5961
import org.springframework.data.domain.Sort.Direction;
6062
import org.springframework.data.domain.Sort.Order;
6163
import org.springframework.data.jpa.domain.Specification;
@@ -1243,7 +1245,7 @@ void scrollByExampleOffset() {
12431245
matching().withMatcher("firstname", GenericPropertyMatcher::startsWith).withIgnorePaths("age", "createdAt",
12441246
"dateOfBirth"));
12451247
Window<User> firstWindow = repository.findBy(example,
1246-
q -> q.limit(2).sortBy(Sort.by("firstname")).scroll(OffsetScrollPosition.initial()));
1248+
q -> q.limit(2).sortBy(Sort.by("firstname")).scroll(ScrollPosition.offset()));
12471249

12481250
assertThat(firstWindow).containsExactly(jane1, jane2);
12491251
assertThat(firstWindow.hasNext()).isTrue();
@@ -1269,7 +1271,7 @@ void scrollByExampleKeyset() {
12691271
matching().withMatcher("firstname", GenericPropertyMatcher::startsWith).withIgnorePaths("age", "createdAt",
12701272
"dateOfBirth"));
12711273
Window<User> firstWindow = repository.findBy(example,
1272-
q -> q.limit(1).sortBy(Sort.by("firstname", "emailAddress")).scroll(KeysetScrollPosition.initial()));
1274+
q -> q.limit(1).sortBy(Sort.by("firstname", "emailAddress")).scroll(ScrollPosition.keyset()));
12731275

12741276
assertThat(firstWindow).containsOnly(jane1);
12751277
assertThat(firstWindow.hasNext()).isTrue();
@@ -1295,12 +1297,12 @@ void scrollByExampleKeysetBackward() {
12951297
matching().withMatcher("firstname", GenericPropertyMatcher::startsWith).withIgnorePaths("age", "createdAt",
12961298
"dateOfBirth"));
12971299
Window<User> firstWindow = repository.findBy(example,
1298-
q -> q.limit(4).sortBy(Sort.by("firstname", "emailAddress")).scroll(KeysetScrollPosition.initial()));
1300+
q -> q.limit(4).sortBy(Sort.by("firstname", "emailAddress")).scroll(ScrollPosition.keyset()));
12991301

13001302
KeysetScrollPosition scrollPosition = (KeysetScrollPosition) firstWindow.positionAt(2);
13011303
Window<User> previousWindow = repository.findBy(example,
13021304
q -> q.limit(1).sortBy(Sort.by("firstname", "emailAddress"))
1303-
.scroll(KeysetScrollPosition.of(scrollPosition.getKeys(), KeysetScrollPosition.Direction.Backward)));
1305+
.scroll(ScrollPosition.backward(scrollPosition.getKeys())));
13041306

13051307
assertThat(previousWindow).containsOnly(jane2);
13061308
assertThat(previousWindow.hasNext()).isTrue();
@@ -1317,7 +1319,7 @@ void scrollByPredicateOffset() {
13171319
repository.saveAllAndFlush(Arrays.asList(john1, john2, jane1, jane2));
13181320

13191321
Window<User> firstWindow = repository.findBy(QUser.user.firstname.startsWith("J"),
1320-
q -> q.limit(2).sortBy(Sort.by("firstname")).scroll(OffsetScrollPosition.initial()));
1322+
q -> q.limit(2).sortBy(Sort.by("firstname")).scroll(ScrollPosition.offset()));
13211323

13221324
assertThat(firstWindow).containsExactly(jane1, jane2);
13231325
assertThat(firstWindow.hasNext()).isTrue();
@@ -1340,7 +1342,7 @@ void scrollByPredicateKeyset() {
13401342
repository.saveAllAndFlush(Arrays.asList(john1, john2, jane1, jane2));
13411343

13421344
Window<User> firstWindow = repository.findBy(QUser.user.firstname.startsWith("J"),
1343-
q -> q.limit(1).sortBy(Sort.by("firstname", "emailAddress")).scroll(KeysetScrollPosition.initial()));
1345+
q -> q.limit(1).sortBy(Sort.by("firstname", "emailAddress")).scroll(ScrollPosition.keyset()));
13441346

13451347
assertThat(firstWindow).containsOnly(jane1);
13461348
assertThat(firstWindow.hasNext()).isTrue();
@@ -1363,16 +1365,14 @@ void scrollByPredicateKeysetBackward() {
13631365
repository.saveAllAndFlush(Arrays.asList(john1, john2, jane1, jane2));
13641366

13651367
Window<User> firstWindow = repository.findBy(QUser.user.firstname.startsWith("J"),
1366-
q -> q.limit(3).sortBy(Sort.by("firstname", "emailAddress")).scroll(KeysetScrollPosition.initial()));
1368+
q -> q.limit(3).sortBy(Sort.by("firstname", "emailAddress")).scroll(ScrollPosition.keyset()));
13671369

13681370
assertThat(firstWindow).containsExactly(jane1, jane2, john1);
13691371
assertThat(firstWindow.hasNext()).isTrue();
13701372

13711373
KeysetScrollPosition scrollPosition = (KeysetScrollPosition) firstWindow.positionAt(2);
1372-
KeysetScrollPosition backward = KeysetScrollPosition.of(scrollPosition.getKeys(),
1373-
KeysetScrollPosition.Direction.Backward);
13741374
Window<User> previousWindow = repository.findBy(QUser.user.firstname.startsWith("J"),
1375-
q -> q.limit(3).sortBy(Sort.by("firstname", "emailAddress")).scroll(backward));
1375+
q -> q.limit(3).sortBy(Sort.by("firstname", "emailAddress")).scroll(scrollPosition.backward()));
13761376

13771377
assertThat(previousWindow).containsExactly(jane1, jane2);
13781378

@@ -1391,16 +1391,14 @@ void scrollByPartTreeKeysetBackward() {
13911391
repository.saveAllAndFlush(Arrays.asList(john1, john2, jane1, jane2));
13921392

13931393
Window<User> firstWindow = repository.findTop3ByFirstnameStartingWithOrderByFirstnameAscEmailAddressAsc("J",
1394-
KeysetScrollPosition.initial());
1394+
ScrollPosition.keyset());
13951395

13961396
assertThat(firstWindow).containsExactly(jane1, jane2, john1);
13971397
assertThat(firstWindow.hasNext()).isTrue();
13981398

13991399
KeysetScrollPosition scrollPosition = (KeysetScrollPosition) firstWindow.positionAt(2);
1400-
KeysetScrollPosition backward = KeysetScrollPosition.of(scrollPosition.getKeys(),
1401-
KeysetScrollPosition.Direction.Backward);
14021400
Window<User> previousWindow = repository.findTop3ByFirstnameStartingWithOrderByFirstnameAscEmailAddressAsc("J",
1403-
backward);
1401+
scrollPosition.backward());
14041402

14051403
assertThat(previousWindow).containsExactly(jane1, jane2);
14061404

@@ -1914,7 +1912,7 @@ void findByCollectionWithPageRequest() {
19141912

19151913
flushTestUsers();
19161914

1917-
Page<User> userPage = repository.findByAgeIn(List.of(28, 35), (PageRequest) PageRequest.of(0, 2));
1915+
Page<User> userPage = repository.findByAgeIn(List.of(28, 35), PageRequest.of(0, 2));
19181916

19191917
assertThat(userPage).hasSize(2);
19201918
assertThat(userPage.getTotalElements()).isEqualTo(2);

0 commit comments

Comments
 (0)