Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sorting ability for unpaged request in QuerydslPredicateExecutor [DATACMNS-1405] #3761

Closed
spring-projects-issues opened this issue Oct 12, 2018 · 3 comments
Assignees
Labels
in: repository Repositories abstraction type: bug A general bug

Comments

@spring-projects-issues
Copy link

Vladyslav Tkachuk opened DATACMNS-1405 and commented

I'm sorry for a bit confusing title, but I'll try to explain it here.

So, org.springframework.data.querydsl.QuerydslPredicateExecutor has a method to return paged data Page findAll(Predicate predicate, Pageable pageable).

If you want to get all existing entities as one page, Pageable.unpaged() as a second paramater.

However, this way you cannot get this single page sorted anyhow, whereas QPageRequest and PageRequest which extend AbstractPageRequest which implements Pageable have sorting ability.

Based on this, if you want to have a single page with all entities, you won't get it sorted.

To my mind this functionality is missing. 

I would be interested to hear your opinions and ideas.

 


No further details from DATACMNS-1405

@spring-projects-issues
Copy link
Author

Oskars Pakers commented

This would be very beneficial to apply default sorting when pagination is not used (but can be passed). A simple class can be added.

class UnpagedSorted implements Pageable {
    private final Sort sort;

    UnpagedSorted(Sort sort) {
        this.sort = sort;
    }

    public boolean isPaged() {
        return false;
    }

    public Pageable previousOrFirst() {
        return this;
    }

    public Pageable next() {
        return this;
    }

    public boolean hasPrevious() {
        return false;
    }

    public Sort getSort() {
        return sort;
    }

    public int getPageSize() {
        throw new UnsupportedOperationException();
    }

    public int getPageNumber() {
        throw new UnsupportedOperationException();
    }

    public long getOffset() {
        throw new UnsupportedOperationException();
    }

    public Pageable first() {
        return this;
    }
}
 

Then service method can use the same repository method, for example.

if (limit == null) {
    return songRepository.findByArtist(artist, new UnpagedSorted(Sort.by("title").ascending()));
}
return songRepository.findByArtist(artist, PageRequest.of(0, limit)); 

What do you think?
If this is ok, I could submit a pull request

@OrangeDog
Copy link

I was also very surprised to find that this does not work:

repository.findAll(predicate, Pageable.unpaged(Sort.by("timestamp")))

The results are instead in the default, unsorted order.
@mp911de shouldn't that be considered a bug?

@mp911de
Copy link
Member

mp911de commented Feb 5, 2025

In JPA, this is still present and we've fixed #3476 for Specifications and Query by Example. I'm going to move this ticket into JPA.

@mp911de mp911de assigned mp911de and unassigned odrotbohm Feb 5, 2025
@mp911de mp911de added type: bug A general bug and removed type: enhancement A general enhancement labels Feb 5, 2025
@mp911de mp911de transferred this issue from spring-projects/spring-data-commons Feb 5, 2025
mp911de added a commit that referenced this issue Feb 5, 2025
We now apply sorting using Querydsl for Pageable that is sorted but not paged.

Closes #3761
mp911de added a commit that referenced this issue Feb 5, 2025
We now apply sorting using Querydsl for Pageable that is sorted but not paged.

Closes #3761
@mp911de mp911de closed this as completed in b90ab18 Feb 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: repository Repositories abstraction type: bug A general bug
Projects
None yet
Development

No branches or pull requests

4 participants