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
2.98k stars 1.41k forks source link

Sorting of Pageable.unpaged(sort) is ignored by JPA Repository #3476

Closed azizairo closed 2 months ago

azizairo commented 4 months ago

I have case when i need unpaged result with sort order. So to implement this i just used Pageable.unpaged method that allows to pass Sort parameter and I am expacting that jpa repository will return unpaged but sorted result. But it is not working cause jpa repostiory ignores sorting for unpaged request:

@Override
    public Page<T> findAll(Pageable pageable) {

        if (pageable.isUnpaged()) {
            return new PageImpl<>(findAll());
        }

        return findAll((Specification<T>) null, pageable);
    }

Is it possible to change behavior to this:

@Override
    public Page<T> findAll(Pageable pageable) {

        if (pageable.isUnpaged()) {
                        if(pageable.getSort().isSorted()) {
                              return new PageImpl<>(findAll(pageable.getSort()));
                        }
            return new PageImpl<>(findAll());
        }

        return findAll((Specification<T>) null, pageable);
    }
quaff commented 4 months ago

I think it's reasonable, It's better to submit a PR instead.

azizairo commented 4 months ago

Ok, I will try to work on PR