spring-projects / spring-data-jpa

Simplifies the development of creating a JPA-based data access layer.
https://spring.io/projects/spring-data-jpa/
Apache License 2.0
3.02k stars 1.42k forks source link

PagingAndSortingRepository.findAll(Pageable) ignores an unpaged Pageable with a sorted Sort #3676

Closed demonfiddler closed 1 week ago

demonfiddler commented 1 week ago

Passing an unpaged Pageable containing a sorted Sort to the PagingAndSortingRepository.findAll(Pageable) method ignores the Pageable's Sort, generating an SQL SELECT statement that is missing the requisite ORDER BY clause. The following code fragment illustrates the problem:

List<Order> orders = List.of(Order.asc("sortProperty"));
Sort sort = Sort.by(orders);
Pageable pageable = Pageable.unpaged(sort);
ListPagingAndSortingRepository<T, ID> repository = ...;
Page<T> unsortedPage = repository.findAll(pageable);
List<T> sortedContent = repository.findAll(sort);
assert unsortedPage.getContent().equals(sortedContent) : "The findAll(Pageable) overload does not sort the content";
mp911de commented 1 week ago

In which project and version did you experience this issue? In JPA, we added this enhancement with 3.4 M1 (spring-projects/spring-data-jpa#3476)

demonfiddler commented 1 week ago

My apologies, it was remiss of me to omit the version info. I am using Spring Boot version 3.3.5 and the problem manifests through spring-boot-starter-data-jpa. Sounds like I should upgrade to 3.4 once it's released. In the meantime I have a workaround which is to call the findAll(Sort) method when an unsorted Pageable is passed.

(BTW before raising this issue I did search the spring-data-commons project for any issues relating to PagingAndSortingRepository but didn't find any of relevance.)

mp911de commented 1 week ago

No worries. It could be that the issue affects other modules as well. Closing this one and moving it to JPA.