Spring Repository自动生成的方法:按给定的最高编号选择+按字段desc排序 [英] Spring Repository auto-generated method: select by given top number + order by field desc

查看:58
本文介绍了Spring Repository自动生成的方法:按给定的最高编号选择+按字段desc排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

春季版本为2.4.0.

我有一个PlayerEntityInteger totalRacesCount字段.我需要按totalRacesCount desc的顺序排列不同顶部长度的播放器顶部.顶部尺寸必须有所不同.

I have a PlayerEntity with Integer totalRacesCount field. I need to make the top of players of different top length ordered by totalRacesCount desc. Top size must vary.

我正在尝试在CrudRepository<PlayerEntity, Long>中添加一种方法来实现此目的.

I am trying to add a method in my CrudRepository<PlayerEntity, Long> to implement this.

https: //docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.limit-query-result -本节指出,我们可以传递数值top来生成可参数化的极限.这是可行的,但仅当不与OrderByTotalRacesCountDesc结合使用时才有效,即,它适用于Asc排序.

https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.limit-query-result — this section states that we can pass a numeric value top to make the limit parameterizable. This is working, but only when not combined with OrderByTotalRacesCountDesc, i.e. it works for Asc sorting.

  • 单个前1条记录的方法(没有top参数有效):PlayerEntity findTopByOrderByTotalRacesCountDesc();-选择最大totalRacesCount的播放器,一切正确.
  • 用于硬编码前N条记录的方法也可以使用:List<PlayerEntity> findTop25ByOrderByTotalRacesCountDesc();-返回25个具有最高totalRacesCount的玩家.
  • 但是,当我尝试添加int top参数:List<PlayerEntity> findTopByOrderByTotalRacesCountDesc(int top);时,应用程序启动失败,并没有期望top参数:
  • Method for a single top 1 record, without top parameter is working: PlayerEntity findTopByOrderByTotalRacesCountDesc(); — selects player with maximal totalRacesCount, everything correct.
  • Method for a hardcoded top N records is also working: List<PlayerEntity> findTop25ByOrderByTotalRacesCountDesc(); — returns 25 players with highest totalRacesCount.
  • However, when I try to add an int top parameter: List<PlayerEntity> findTopByOrderByTotalRacesCountDesc(int top);, start of the application fails with non-expecting the top parameter:
java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List parser.repository.PlayerRepository.findTopByOrderByTotalRacesCountDesc(int)! At least 1 parameter(s) provided but only 0 parameter(s) present in query.

  • 我还尝试在Top:List<PlayerEntity> findTopOrderByTotalRacesCountDesc(int top);之后省略By,但这显然是不正确的语法,并且无法将Desc当作属性名:
    • I've also tried to omit By after Top: List<PlayerEntity> findTopOrderByTotalRacesCountDesc(int top);, but this obviously is an incorrect syntax and fails on treating Desc as property name:
    •  java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List parser.PlayerRepository.findTopOrderByTotalRacesCountDesc(int)! No property desc found for type Integer!
      

      我知道我可以实现@Query并将top设置为@Param.但是可以通过方法名称自动生成的查询来实现任务吗? 该行为实际上看起来像是个错误.

      I know I can implement the @Query and set top as a @Param. But is it possible to implement the task with the query auto-generated by method name? The behavior looks like a bug actually.

      我找到的所有链接都使用@Query(JPA或本机)来实现order by desc排序.

      All the links I've found use @Query (JPA or native) to implement the order by desc sorting.

      推荐答案

      好像我没有正确阅读文档:

      Looks like I was reading the documentation incorrectly:

      您可以在顶部或顶部附加一个可选的数值,以指定要返回的最大结果大小.

      You can append an optional numeric value to top or first to specify the maximum result size to be returned.

      这实际上并不意味着方法参数"可以有所不同,但只是方法名Top之后的硬编码数字.

      This actually DOES NOT mean a "method parameter" that you can vary, but just a hard-coded number after Top in the method name.

      解决方案::我应该改用setMaxResults.

      这篇关于Spring Repository自动生成的方法:按给定的最高编号选择+按字段desc排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆