Skip to content

Commit 0206de8

Browse files
christophstroblmp911de
authored andcommitted
Align OffsetScrolling to zero-based indexes.
Closes: #3409 Original pull request: #3415
1 parent 2e8c7c1 commit 0206de8

File tree

7 files changed

+27
-10
lines changed

7 files changed

+27
-10
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,8 @@ public Query createQuery(JpaParametersParameterAccessor accessor) {
259259
@SuppressWarnings("ConstantConditions")
260260
private Query restrictMaxResultsIfNecessary(Query query, @Nullable ScrollPosition scrollPosition) {
261261

262-
if (scrollPosition instanceof OffsetScrollPosition offset) {
263-
query.setFirstResult(Math.toIntExact(offset.getOffset()));
262+
if (scrollPosition instanceof OffsetScrollPosition offset && !offset.isInitial()) {
263+
query.setFirstResult(Math.toIntExact(offset.getOffset()) + 1);
264264
}
265265

266266
if (tree.isLimiting()) {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public Window<T> scroll(Query query, Sort sort, ScrollPosition scrollPosition) {
7171
}
7272

7373
if (scrollPosition instanceof OffsetScrollPosition offset) {
74-
return createWindow(result, limit, OffsetScrollPosition.positionFunction(offset.getOffset()));
74+
return createWindow(result, limit, OffsetScrollPosition.positionFunction(offset.isInitial() ? 0 : offset.getOffset()));
7575
}
7676

7777
throw new UnsupportedOperationException("ScrollPosition " + scrollPosition + " not supported");

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/QuerydslJpaPredicateExecutor.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,9 @@ public <S extends T, R> R findBy(Predicate predicate, Function<FetchableFluentQu
196196
select = (AbstractJPAQuery<?, ?>) querydsl.applySorting(sort, select);
197197

198198
if (scrollPosition instanceof OffsetScrollPosition offset) {
199-
select.offset(offset.getOffset());
199+
if(!offset.isInitial()) {
200+
select.offset(offset.getOffset() + 1);
201+
}
200202
}
201203

202204
return select.createQuery();

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,9 @@ private <S extends T, R> R doFindBy(Specification<T> spec, Class<T> domainClass,
512512
TypedQuery<T> query = getQuery(specToUse, domainClass, sort);
513513

514514
if (scrollPosition instanceof OffsetScrollPosition offset) {
515-
query.setFirstResult(Math.toIntExact(offset.getOffset()));
515+
if(!offset.isInitial()) {
516+
query.setFirstResult(Math.toIntExact(offset.getOffset()) + 1);
517+
}
516518
}
517519

518520
return query;

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

+14-3
Original file line numberDiff line numberDiff line change
@@ -226,23 +226,34 @@ void executesQueryToSliceWithUnpaged() {
226226
assertThat(slice.hasNext()).isFalse();
227227
}
228228

229-
@Test // DATAJPA-94
229+
@Test // GH-3077, GH-3409
230230
void executesQueryWithLimitAndScrollPosition() {
231231

232232
Window<User> first = userRepository.findByLastnameOrderByFirstname(Limit.of(1), //
233-
ScrollPosition.offset(), //
233+
ScrollPosition.offset(), // initial position no offset
234234
"Matthews" //
235235
);
236236

237237
Window<User> next = userRepository.findByLastnameOrderByFirstname(Limit.of(1), //
238-
ScrollPosition.offset(1), //
238+
ScrollPosition.offset(0), // first position, offset = 1
239239
"Matthews" //
240240
);
241241

242242
assertThat(first).containsExactly(dave);
243243
assertThat(next).containsExactly(oliver);
244244
}
245245

246+
@Test // GH-3409
247+
void executesWindowQueryWithPageable() {
248+
249+
Window<User> first = userRepository.findByLastnameOrderByFirstname("Matthews", PageRequest.of(0,1));
250+
251+
Window<User> next = userRepository.findByLastnameOrderByFirstname("Matthews", PageRequest.of(1,1));
252+
253+
assertThat(first).containsExactly(dave);
254+
assertThat(next).containsExactly(oliver);
255+
}
256+
246257
@Test // GH-3077
247258
void shouldProjectWithKeysetScrolling() {
248259

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1430,7 +1430,7 @@ void scrollByPartTreeKeysetBackward() {
14301430
assertThat(previousWindow.hasNext()).isFalse();
14311431
}
14321432

1433-
@Test // GH-3015
1433+
@Test // GH-3015, GH-3407
14341434
void shouldApplyOffsetScrollPosition() {
14351435

14361436
User jane1 = new User("Jane", "Doe", "[email protected]");
@@ -1441,7 +1441,7 @@ void shouldApplyOffsetScrollPosition() {
14411441
repository.saveAllAndFlush(Arrays.asList(john1, john2, jane1, jane2));
14421442

14431443
Window<User> atOffset3 = repository.findByFirstnameStartingWithOrderByFirstnameAscEmailAddressAsc("J",
1444-
ScrollPosition.offset(3));
1444+
ScrollPosition.offset(2));
14451445

14461446
assertThat(atOffset3).containsExactly(john2);
14471447
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ Window<User> findTop3ByFirstnameStartingWithOrderByFirstnameAscEmailAddressAsc(S
197197

198198
Window<User> findByLastnameOrderByFirstname(Limit limit, ScrollPosition scrollPosition, String lastname);
199199

200+
Window<User> findByLastnameOrderByFirstname(String lastname, Pageable page);
201+
200202
Window<NameOnly> findTop1ByLastnameOrderByFirstname(ScrollPosition scrollPosition, String lastname);
201203

202204
List<User> findByLastnameIgnoringCaseLike(String lastname);

0 commit comments

Comments
 (0)