简体   繁体   English

如何创建未分页但已排序的 Pageable.of(unpaged, unpaged, Sort ) 到 spring 数据 jpa 存储库?

[英]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?如何将可分页 object 传递给Pageable JPA 存储库,仅包含排序信息? 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我知道它需要int作为pagesize ,但是如果我们不能以某种方式不使用这些值并仅给出未分页的结果

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.我们可以使用像10001000这样的大数字来表示页面和大小,但我不想硬编码这些值。

I am also not looking to create a custom impl for Pageable .我也不打算为Pageable创建自定义 impl。

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:根据Spring 数据 JPA 当前版本的文档,您可以将Sort直接传递给查询方法,因此您可以有两个版本的相同查询方法:

  • One receiving a Pageable , for use when you have a "full" Pageable .一个接收Pageable ,当你有一个“完整的” Pageable时使用。
  • Another one receiving a Sort , for use when you just want to perform sorting.另一个接收Sort ,当您只想执行排序时使用。

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.如果您坚持使用现有查询,则可以在查询中传递Pageable.unpaged()然后对结果进行排序( list.getContent().sort(Comparator.comparing(Item::getName)) )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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