Skip to content

Commit fb4214c

Browse files
committed
Correctly apply OffsetScrollPosition to derived queries.
We now apply the scroll position correctly regardless of whether the query is limited. Previously, we applied the position only if the query was limited. Closes #3015
1 parent 6eda785 commit fb4214c

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,11 @@ public Query createQuery(JpaParametersParameterAccessor accessor) {
255255
@SuppressWarnings("ConstantConditions")
256256
private Query restrictMaxResultsIfNecessary(Query query, @Nullable ScrollPosition scrollPosition) {
257257

258-
if (tree.isLimiting()) {
258+
if (scrollPosition instanceof OffsetScrollPosition offset) {
259+
query.setFirstResult(Math.toIntExact(offset.getOffset()));
260+
}
259261

260-
if (scrollPosition instanceof OffsetScrollPosition offset) {
261-
query.setFirstResult(Math.toIntExact(offset.getOffset()));
262-
}
262+
if (tree.isLimiting()) {
263263

264264
if (query.getMaxResults() != Integer.MAX_VALUE) {
265265
/*

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

+16-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
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.*;
6463
import org.springframework.data.jpa.domain.Specification;
6564
import org.springframework.data.jpa.domain.sample.Address;
6665
import org.springframework.data.jpa.domain.sample.QUser;
@@ -1432,6 +1431,22 @@ void scrollByPartTreeKeysetBackward() {
14321431
assertThat(previousWindow.hasNext()).isFalse();
14331432
}
14341433

1434+
@Test // GH-3015
1435+
void shouldApplyOffsetScrollPosition() {
1436+
1437+
User jane1 = new User("Jane", "Doe", "[email protected]");
1438+
User jane2 = new User("Jane", "Doe", "[email protected]");
1439+
User john1 = new User("John", "Doe", "[email protected]");
1440+
User john2 = new User("John", "Doe", "[email protected]");
1441+
1442+
repository.saveAllAndFlush(Arrays.asList(john1, john2, jane1, jane2));
1443+
1444+
Window<User> atOffset3 = repository.findByFirstnameStartingWithOrderByFirstnameAscEmailAddressAsc("J",
1445+
ScrollPosition.offset(3));
1446+
1447+
assertThat(atOffset3).containsExactly(john2);
1448+
}
1449+
14351450
@Test // DATAJPA-491
14361451
void sortByNestedAssociationPropertyWithSortInPageable() {
14371452

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

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.Set;
2727
import java.util.stream.Stream;
2828

29+
import org.springframework.data.domain.OffsetScrollPosition;
2930
import org.springframework.data.domain.Page;
3031
import org.springframework.data.domain.PageRequest;
3132
import org.springframework.data.domain.Pageable;
@@ -155,6 +156,8 @@ public interface UserRepository extends JpaRepository<User, Integer>, JpaSpecifi
155156
Window<User> findTop3ByFirstnameStartingWithOrderByFirstnameAscEmailAddressAsc(String firstname,
156157
ScrollPosition position);
157158

159+
Window<User> findByFirstnameStartingWithOrderByFirstnameAscEmailAddressAsc(String firstname, ScrollPosition position);
160+
158161
List<User> findByFirstnameNotIn(Collection<String> firstnames);
159162

160163
// DATAJPA-292
@@ -722,6 +725,8 @@ List<String> findAllAndSortByFunctionResultNamedParameter(@Param("namedParameter
722725
@Query("select u from User u where u.firstname >= (select Min(u0.firstname) from User u0)")
723726
List<NameOnly> findProjectionBySubselect();
724727

728+
Window<User> findBy(OffsetScrollPosition position);
729+
725730
interface RolesAndFirstname {
726731

727732
String getFirstname();

0 commit comments

Comments
 (0)