Skip to content

Commit b90ab18

Browse files
committedFeb 5, 2025
Consider sort order for unpaged Pageable in Querydsl.
We now apply sorting using Querydsl for Pageable that is sorted but not paged. Closes #3761
1 parent 8571056 commit b90ab18

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed
 

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

+4-5
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,11 @@ public <T> JPQLQuery<T> applyPagination(Pageable pageable, JPQLQuery<T> query) {
124124
Assert.notNull(pageable, "Pageable must not be null");
125125
Assert.notNull(query, "JPQLQuery must not be null");
126126

127-
if (pageable.isUnpaged()) {
128-
return query;
129-
}
127+
if (pageable.isPaged()) {
130128

131-
query.offset(pageable.getOffset());
132-
query.limit(pageable.getPageSize());
129+
query.offset(pageable.getOffset());
130+
query.limit(pageable.getPageSize());
131+
}
133132

134133
return applySorting(pageable.getSort(), query);
135134
}

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

-2
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,6 @@ protected <S extends T> Page<S> readPage(TypedQuery<S> query, Class<S> domainCla
738738
* @param pageable must not be {@literal null}.
739739
*/
740740
protected TypedQuery<T> getQuery(@Nullable Specification<T> spec, Pageable pageable) {
741-
742741
return getQuery(spec, getDomainClass(), pageable.getSort());
743742
}
744743

@@ -751,7 +750,6 @@ protected TypedQuery<T> getQuery(@Nullable Specification<T> spec, Pageable pagea
751750
*/
752751
protected <S extends T> TypedQuery<S> getQuery(@Nullable Specification<S> spec, Class<S> domainClass,
753752
Pageable pageable) {
754-
755753
return getQuery(spec, domainClass, pageable.getSort());
756754
}
757755

‎spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/support/QuerydslJpaPredicateExecutorUnitTests.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,15 @@ void shouldSupportFindAllWithPredicateAndSort() {
282282
assertThat(users).contains(carter, dave, oliver);
283283
}
284284

285-
@Test // DATAJPA-585
285+
@Test // DATAJPA-585, 3761
286286
void worksWithUnpagedPageable() {
287+
287288
assertThat(predicateExecutor.findAll(user.dateOfBirth.isNull(), Pageable.unpaged()).getContent()).hasSize(3);
289+
290+
Page<User> users = predicateExecutor.findAll(user.dateOfBirth.isNull(),
291+
Pageable.unpaged(Sort.by(Direction.ASC, "firstname")));
292+
293+
assertThat(users).containsExactly(carter, dave, oliver);
288294
}
289295

290296
@Test // DATAJPA-912

0 commit comments

Comments
 (0)
Please sign in to comment.