Skip to content

Commit 38a11d0

Browse files
birarirochristophstrobl
authored andcommitted
Apply sort from unpaged Pageable to query.
This commit makes sure to pass on a given Sort from an unpaged Pageable to the actual query. Closes: #3476 Original Pull Request: #3517
1 parent c0ae93c commit 38a11d0

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java

+2-9
Original file line numberDiff line numberDiff line change
@@ -426,11 +426,6 @@ public List<T> findAll(Sort sort) {
426426

427427
@Override
428428
public Page<T> findAll(Pageable pageable) {
429-
430-
if (pageable.isUnpaged()) {
431-
return new PageImpl<>(findAll());
432-
}
433-
434429
return findAll((Specification<T>) null, pageable);
435430
}
436431

@@ -717,8 +712,7 @@ protected <S extends T> Page<S> readPage(TypedQuery<S> query, final Class<S> dom
717712
*/
718713
protected TypedQuery<T> getQuery(@Nullable Specification<T> spec, Pageable pageable) {
719714

720-
Sort sort = pageable.isPaged() ? pageable.getSort() : Sort.unsorted();
721-
return getQuery(spec, getDomainClass(), sort);
715+
return getQuery(spec, getDomainClass(), pageable.getSort());
722716
}
723717

724718
/**
@@ -731,8 +725,7 @@ protected TypedQuery<T> getQuery(@Nullable Specification<T> spec, Pageable pagea
731725
protected <S extends T> TypedQuery<S> getQuery(@Nullable Specification<S> spec, Class<S> domainClass,
732726
Pageable pageable) {
733727

734-
Sort sort = pageable.isPaged() ? pageable.getSort() : Sort.unsorted();
735-
return getQuery(spec, domainClass, sort);
728+
return getQuery(spec, domainClass, pageable.getSort());
736729
}
737730

738731
/**

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

+12
Original file line numberDiff line numberDiff line change
@@ -2887,6 +2887,18 @@ void existsByExampleNegative() {
28872887
assertThat(exists).isFalse();
28882888
}
28892889

2890+
@Test // GH-3476
2891+
void unPagedSortedQuery() {
2892+
2893+
flushTestUsers();
2894+
2895+
Sort sort = Sort.by(DESC, "firstname");
2896+
Page<User> firstPage = repository.findAll(PageRequest.of(0, 10, sort));
2897+
Page<User> secondPage = repository.findAll(Pageable.unpaged(sort));
2898+
assertThat(firstPage.getContent()).isEqualTo(secondPage.getContent());
2899+
}
2900+
2901+
28902902
@Test // DATAJPA-905
28912903
void executesPagedSpecificationSettingAnOrder() {
28922904

0 commit comments

Comments
 (0)