DATAJPA-1554 - Fixed handling of unpaged for return type Slice.

Original Pull Request: #383
This commit is contained in:
Jens Schauder
2019-06-11 10:40:32 +02:00
committed by Christoph Strobl
parent 2fbfc0cf41
commit d8c282ec9a
2 changed files with 24 additions and 6 deletions

View File

@@ -161,13 +161,20 @@ public abstract class JpaQueryExecution {
Pageable pageable = accessor.getPageable();
Query createQuery = query.createQuery(values);
int pageSize = pageable.getPageSize();
createQuery.setMaxResults(pageSize + 1);
int pageSize = 0;
if (pageable.isPaged()) {
pageSize = pageable.getPageSize();
createQuery.setMaxResults(pageSize + 1);
}
List<Object> resultList = createQuery.getResultList();
boolean hasNext = resultList.size() > pageSize;
return new SliceImpl<Object>(hasNext ? resultList.subList(0, pageSize) : resultList, pageable, hasNext);
boolean hasNext = pageable.isPaged() && resultList.size() > pageSize;
return new SliceImpl<>(hasNext ? resultList.subList(0, pageSize) : resultList, pageable, hasNext);
}
}
@@ -225,8 +232,8 @@ public abstract class JpaQueryExecution {
private final boolean clear;
/**
* Creates an execution that automatically flushes the given {@link EntityManager} before execution and/or
* clears the given {@link EntityManager} after execution.
* Creates an execution that automatically flushes the given {@link EntityManager} before execution and/or clears
* the given {@link EntityManager} after execution.
*
* @param em Must not be {@literal null}.
*/

View File

@@ -30,6 +30,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.sample.Role;
@@ -193,6 +194,16 @@ public class UserRepositoryFinderTests {
assertThat(slice.hasNext(), is(true));
}
@Test // DATAJPA-1554
public void executesQueryToSliceWithUnpaged() {
Slice<User> slice = userRepository.findSliceByLastname("Matthews", Pageable.unpaged());
assertThat(slice, containsInAnyOrder(dave, oliver));
assertThat(slice.getNumberOfElements(), is(2));
assertThat(slice.hasNext(), is(false));
}
@Test // DATAJPA-830
public void executesMethodWithNotContainingOnStringCorrectly() {
assertThat(userRepository.findByLastnameNotContaining("u"), containsInAnyOrder(dave, oliver));