如何在Spring Data(JPA)派生查询中按多个属性排序?
I'm looking at the examples giving on this page (http://docs.spring.io/spring-data/jpa/docs/current/reference/html/jpa.repositories.html) about method naming, is it possible to create a complex chain method name such as
我正在查看关于方法命名的本页(http://docs.spring.io/spring-data/jpa/docs/current/reference/html/jpa.repositories.html)上给出的示例,是否可以创建一个复杂的链方法名称,如
findByProgrammeAndDirectorAndProgDateBetweenOrderByProgDateStartTimeAsc
In the example they give, they are only doing an OrderBy on one value. In the example above ProgDate
and StartTime
would be two separate values.
在他们给出的示例中,他们只在一个值上执行OrderBy。在上面的示例中,ProgDate和StartTime将是两个单独的值。
2 个解决方案
#1
76
The trick is to simply delimit the properties you want to sort by using the direction keywords Asc
and Desc
. So what you probably want in your query method is something like:
诀窍是使用方向关键字Asc和Desc简单地分隔要排序的属性。那么您在查询方法中可能需要的是:
…OrderByProgDateAscStartTimeAsc
Note, how we conclude the first property definition by Asc
and keep going with the next property.
注意,我们如何通过Asc结束第一个属性定义并继续使用下一个属性。
Generally speaking, we recommend switching to @Query
based queries, once method names exceed a certain length or complexity. The main reason being that it's awkward for clients to call these very long methods. With @Query
you rather get the full power of the query language plus a reasonably sized method name that might be of higher level language to express the intent of the query.
一般来说,一旦方法名称超过一定的长度或复杂性,我们建议切换到基于@Query的查询。主要原因是客户称这些非常长的方法很尴尬。使用@Query,您可以获得查询语言的全部功能以及可能具有更高级语言的合理大小的方法名称来表达查询的意图。