Skip to content

Align OffsetScrolling to zero-based indexes #3415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.x-3409-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data JPA Parent</name>
Expand Down Expand Up @@ -41,7 +41,7 @@
<jsqlparser>4.8</jsqlparser>
<mysql-connector-java>8.0.33</mysql-connector-java>
<postgresql>42.6.0</postgresql>
<springdata.commons>3.3.0-SNAPSHOT</springdata.commons>
<springdata.commons>3.3.x-3070-SNAPSHOT</springdata.commons>
<vavr>0.10.3</vavr>

<hibernate.groupId>org.hibernate</hibernate.groupId>
Expand Down
4 changes: 2 additions & 2 deletions spring-data-envers/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-envers</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.x-3409-SNAPSHOT</version>

<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.x-3409-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-jpa-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.x-3409-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.x-3409-SNAPSHOT</version>

<name>Spring Data JPA</name>
<description>Spring Data module for JPA repositories.</description>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.x-3409-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ public Query createQuery(JpaParametersParameterAccessor accessor) {
@SuppressWarnings("ConstantConditions")
private Query restrictMaxResultsIfNecessary(Query query, @Nullable ScrollPosition scrollPosition) {

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

if (tree.isLimiting()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public Window<T> scroll(Query query, Sort sort, ScrollPosition scrollPosition) {
}

if (scrollPosition instanceof OffsetScrollPosition offset) {
return createWindow(result, limit, OffsetScrollPosition.positionFunction(offset.getOffset()));
return createWindow(result, limit, OffsetScrollPosition.positionFunction(offset.isInitial() ? 0 : offset.getOffset()));
}

throw new UnsupportedOperationException("ScrollPosition " + scrollPosition + " not supported");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,9 @@ public <S extends T, R> R findBy(Predicate predicate, Function<FetchableFluentQu
select = (AbstractJPAQuery<?, ?>) querydsl.applySorting(sort, select);

if (scrollPosition instanceof OffsetScrollPosition offset) {
select.offset(offset.getOffset());
if(!offset.isInitial()) {
select.offset(offset.getOffset() + 1);
}
}

return select.createQuery();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,9 @@ private <S extends T, R> R doFindBy(Specification<T> spec, Class<T> domainClass,
TypedQuery<T> query = getQuery(specToUse, domainClass, sort);

if (scrollPosition instanceof OffsetScrollPosition offset) {
query.setFirstResult(Math.toIntExact(offset.getOffset()));
if(!offset.isInitial()) {
query.setFirstResult(Math.toIntExact(offset.getOffset()) + 1);
}
}

return query;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,23 +226,34 @@ void executesQueryToSliceWithUnpaged() {
assertThat(slice.hasNext()).isFalse();
}

@Test // DATAJPA-94
@Test // GH-3077, GH-3409
void executesQueryWithLimitAndScrollPosition() {

Window<User> first = userRepository.findByLastnameOrderByFirstname(Limit.of(1), //
ScrollPosition.offset(), //
ScrollPosition.offset(), // initial position no offset
"Matthews" //
);

Window<User> next = userRepository.findByLastnameOrderByFirstname(Limit.of(1), //
ScrollPosition.offset(1), //
ScrollPosition.offset(0), // first position, offset = 1
"Matthews" //
);

assertThat(first).containsExactly(dave);
assertThat(next).containsExactly(oliver);
}

@Test // GH-3409
void executesWindowQueryWithPageable() {

Window<User> first = userRepository.findByLastnameOrderByFirstname("Matthews", PageRequest.of(0,1));

Window<User> next = userRepository.findByLastnameOrderByFirstname("Matthews", PageRequest.of(1,1));

assertThat(first).containsExactly(dave);
assertThat(next).containsExactly(oliver);
}

@Test // GH-3077
void shouldProjectWithKeysetScrolling() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1430,7 +1430,7 @@ void scrollByPartTreeKeysetBackward() {
assertThat(previousWindow.hasNext()).isFalse();
}

@Test // GH-3015
@Test // GH-3015, GH-3407
void shouldApplyOffsetScrollPosition() {

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

Window<User> atOffset3 = repository.findByFirstnameStartingWithOrderByFirstnameAscEmailAddressAsc("J",
ScrollPosition.offset(3));
ScrollPosition.offset(2));

assertThat(atOffset3).containsExactly(john2);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ Window<User> findTop3ByFirstnameStartingWithOrderByFirstnameAscEmailAddressAsc(S

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

Window<User> findByLastnameOrderByFirstname(String lastname, Pageable page);

Window<NameOnly> findTop1ByLastnameOrderByFirstname(ScrollPosition scrollPosition, String lastname);

List<User> findByLastnameIgnoringCaseLike(String lastname);
Expand Down