简体   繁体   中英

How to create unpaged but sorted Pageable.of(unpaged, unpaged, Sort ) to spring data jpa repository?

How do I pass a Pageable object to Spring JPA Repository with only Sorting information? I want something like this:

Pageable pageable = PageRequest.of(null, null, Sort());

I know it takes int as page and size but if we can somehow not use those values and give an unpaged result with only

Sort.by(Sort.Direction.ASC, "age")

We could use a high number like 1000 , 1000 for page and size but I do not want to hardcode these values.

I am also not looking to create a custom impl for Pageable .

Please let me know how to achieve this.

Thanks in advance.

According to the documentation for the current version of Spring Data JPA , you can pass a Sort directly to a query method, so you could have two versions of the same query method:

  • One receiving a Pageable , for use when you have a "full" Pageable .
  • Another one receiving a Sort , for use when you just want to perform sorting.

You could pass Pageable.unpaged() in the query and then sort the result ( list.getContent().sort(Comparator.comparing(Item::getName)) ) if you are adamant about using the existing query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM