Skip to content

Commit d5f68c4

Browse files
committed
Allow sorting of unpaged results.
Closes: spring-projects#2691
1 parent 34e16e3 commit d5f68c4

File tree

2 files changed

+79
-5
lines changed

2 files changed

+79
-5
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
@@ -48,6 +48,7 @@
4848
* @author Mark Paluch
4949
* @author Christoph Strobl
5050
* @author Marcus Voltolim
51+
* @author Anatoliy Golubev
5152
*/
5253
public class Querydsl {
5354

@@ -114,13 +115,11 @@ public <T> JPQLQuery<T> applyPagination(Pageable pageable, JPQLQuery<T> query) {
114115
Assert.notNull(pageable, "Pageable must not be null");
115116
Assert.notNull(query, "JPQLQuery must not be null");
116117

117-
if (pageable.isUnpaged()) {
118-
return query;
118+
if (pageable.isPaged()) {
119+
query.offset(pageable.getOffset());
120+
query.limit(pageable.getPageSize());
119121
}
120122

121-
query.offset(pageable.getOffset());
122-
query.limit(pageable.getPageSize());
123-
124123
return applySorting(pageable.getSort(), query);
125124
}
126125

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

+75
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
* @author Christoph Strobl
6565
* @author Malte Mauelshagen
6666
* @author Greg Turnquist
67+
* @author Anatoliy Golubev
6768
*/
6869
@ExtendWith(SpringExtension.class)
6970
@ContextConfiguration({ "classpath:infrastructure.xml" })
@@ -517,6 +518,80 @@ void findByFluentPredicateWithComplexPropertyPathsDoesntLoadsRequestedPaths() {
517518
assertThat(users).allMatch(u -> u.getRoles().isEmpty());
518519
}
519520

521+
@Test // GH-2691
522+
void shouldSupportSortByWithUnpagedPageable() {
523+
524+
// "unpaged" pageable implementation with sort
525+
@Data
526+
class UnpageableWithSort implements Pageable {
527+
private final Sort sort;
528+
529+
@Override
530+
public boolean isPaged() {
531+
return false;
532+
}
533+
534+
@Override
535+
public Pageable previousOrFirst() {
536+
return this;
537+
}
538+
539+
@Override
540+
public Pageable next() {
541+
return this;
542+
}
543+
544+
@Override
545+
public boolean hasPrevious() {
546+
return false;
547+
}
548+
549+
@Override
550+
public Sort getSort() {
551+
return sort;
552+
}
553+
554+
@Override
555+
public int getPageSize() {
556+
throw new UnsupportedOperationException();
557+
}
558+
559+
@Override
560+
public int getPageNumber() {
561+
throw new UnsupportedOperationException();
562+
}
563+
564+
@Override
565+
public long getOffset() {
566+
throw new UnsupportedOperationException();
567+
}
568+
569+
@Override
570+
public Pageable first() {
571+
return this;
572+
}
573+
574+
@Override
575+
public Pageable withPage(int pageNumber) {
576+
if (pageNumber == 0) {
577+
return this;
578+
} else {
579+
throw new UnsupportedOperationException();
580+
}
581+
}
582+
}
583+
584+
Predicate lastnameContainsE = user.lastname.contains("e");
585+
586+
Page<User> result = predicateExecutor.findAll(lastnameContainsE, new UnpageableWithSort(Sort.by("lastname").ascending()));
587+
588+
assertThat(result).containsExactly(carter, dave, oliver);
589+
590+
result = predicateExecutor.findAll(lastnameContainsE, new UnpageableWithSort(Sort.by("lastname").descending()));
591+
592+
assertThat(result).containsExactly(oliver, dave, carter);
593+
}
594+
520595
private interface UserProjectionInterfaceBased {
521596
String getFirstname();
522597

0 commit comments

Comments
 (0)