Skip to content

Commit 37e88e9

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 13a4661 commit 37e88e9

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

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
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.*;
6160
import org.springframework.data.jpa.domain.Specification;
6261
import org.springframework.data.jpa.domain.sample.Address;
6362
import org.springframework.data.jpa.domain.sample.QUser;
@@ -1428,6 +1427,22 @@ void scrollByPartTreeKeysetBackward() {
14281427
assertThat(previousWindow.hasNext()).isFalse();
14291428
}
14301429

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

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

Lines changed: 5 additions & 0 deletions
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)