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
This commit is contained in:
Mark Paluch
2023-06-26 10:33:58 +02:00
parent 6eda785ac7
commit fb4214ca36
3 changed files with 25 additions and 5 deletions

View File

@@ -255,11 +255,11 @@ public class PartTreeJpaQuery extends AbstractJpaQuery {
@SuppressWarnings("ConstantConditions")
private Query restrictMaxResultsIfNecessary(Query query, @Nullable ScrollPosition scrollPosition) {
if (tree.isLimiting()) {
if (scrollPosition instanceof OffsetScrollPosition offset) {
query.setFirstResult(Math.toIntExact(offset.getOffset()));
}
if (scrollPosition instanceof OffsetScrollPosition offset) {
query.setFirstResult(Math.toIntExact(offset.getOffset()));
}
if (tree.isLimiting()) {
if (query.getMaxResults() != Integer.MAX_VALUE) {
/*

View File

@@ -60,7 +60,6 @@ import org.springframework.data.domain.ExampleMatcher.GenericPropertyMatcher;
import org.springframework.data.domain.ExampleMatcher.StringMatcher;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.domain.ExampleMatcher.*;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.domain.sample.Address;
import org.springframework.data.jpa.domain.sample.QUser;
@@ -1432,6 +1431,22 @@ class UserRepositoryTests {
assertThat(previousWindow.hasNext()).isFalse();
}
@Test // GH-3015
void shouldApplyOffsetScrollPosition() {
User jane1 = new User("Jane", "Doe", "jane@doe1.com");
User jane2 = new User("Jane", "Doe", "jane@doe2.com");
User john1 = new User("John", "Doe", "john@doe1.com");
User john2 = new User("John", "Doe", "john@doe2.com");
repository.saveAllAndFlush(Arrays.asList(john1, john2, jane1, jane2));
Window<User> atOffset3 = repository.findByFirstnameStartingWithOrderByFirstnameAscEmailAddressAsc("J",
ScrollPosition.offset(3));
assertThat(atOffset3).containsExactly(john2);
}
@Test // DATAJPA-491
void sortByNestedAssociationPropertyWithSortInPageable() {

View File

@@ -26,6 +26,7 @@ import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;
import org.springframework.data.domain.OffsetScrollPosition;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
@@ -155,6 +156,8 @@ public interface UserRepository extends JpaRepository<User, Integer>, JpaSpecifi
Window<User> findTop3ByFirstnameStartingWithOrderByFirstnameAscEmailAddressAsc(String firstname,
ScrollPosition position);
Window<User> findByFirstnameStartingWithOrderByFirstnameAscEmailAddressAsc(String firstname, ScrollPosition position);
List<User> findByFirstnameNotIn(Collection<String> firstnames);
// DATAJPA-292
@@ -722,6 +725,8 @@ public interface UserRepository extends JpaRepository<User, Integer>, JpaSpecifi
@Query("select u from User u where u.firstname >= (select Min(u0.firstname) from User u0)")
List<NameOnly> findProjectionBySubselect();
Window<User> findBy(OffsetScrollPosition position);
interface RolesAndFirstname {
String getFirstname();