Skip to content

Commit 11be12c

Browse files
birariromp911de
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 d56bb75 commit 11be12c

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

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

+2-10
Original file line numberDiff line numberDiff line change
@@ -430,11 +430,6 @@ public List<T> findAll(Sort sort) {
430430

431431
@Override
432432
public Page<T> findAll(Pageable pageable) {
433-
434-
if (pageable.isUnpaged()) {
435-
return new PageImpl<>(findAll());
436-
}
437-
438433
return findAll((Specification<T>) null, pageable);
439434
}
440435

@@ -728,9 +723,7 @@ protected <S extends T> Page<S> readPage(TypedQuery<S> query, final Class<S> dom
728723
* @param pageable must not be {@literal null}.
729724
*/
730725
protected TypedQuery<T> getQuery(@Nullable Specification<T> spec, Pageable pageable) {
731-
732-
Sort sort = pageable.isPaged() ? pageable.getSort() : Sort.unsorted();
733-
return getQuery(spec, getDomainClass(), sort);
726+
return getQuery(spec, getDomainClass(), pageable.getSort());
734727
}
735728

736729
/**
@@ -743,8 +736,7 @@ protected TypedQuery<T> getQuery(@Nullable Specification<T> spec, Pageable pagea
743736
protected <S extends T> TypedQuery<S> getQuery(@Nullable Specification<S> spec, Class<S> domainClass,
744737
Pageable pageable) {
745738

746-
Sort sort = pageable.isPaged() ? pageable.getSort() : Sort.unsorted();
747-
return getQuery(spec, domainClass, sort);
739+
return getQuery(spec, domainClass, pageable.getSort());
748740
}
749741

750742
/**

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

+12
Original file line numberDiff line numberDiff line change
@@ -2896,6 +2896,18 @@ void existsByExampleNegative() {
28962896
assertThat(exists).isFalse();
28972897
}
28982898

2899+
@Test // GH-3476
2900+
void unPagedSortedQuery() {
2901+
2902+
flushTestUsers();
2903+
2904+
Sort sort = Sort.by(DESC, "firstname");
2905+
Page<User> firstPage = repository.findAll(PageRequest.of(0, 10, sort));
2906+
Page<User> secondPage = repository.findAll(Pageable.unpaged(sort));
2907+
assertThat(firstPage.getContent()).isEqualTo(secondPage.getContent());
2908+
}
2909+
2910+
28992911
@Test // DATAJPA-905
29002912
void executesPagedSpecificationSettingAnOrder() {
29012913

0 commit comments

Comments
 (0)