使用 cast 在 JPQL 的 order by 子句中。

huangapple go评论59阅读模式
英文:

Use cast in order by clause JPQL

问题

在JPQL查询中,我想在ORDER BY子句中使用CAST运算符。该查询在SQL中有效,但应用程序在运行时产生了所示错误。如何在JPQL查询的ORDER BY子句中使用CAST?提前致谢。

@Query("SELECT a FROM tableA a WHERE a.tableB.ID = :ID ORDER BY CAST(col AS unsigned)")

错误信息:

Caused by: org.hibernate.QueryException: 无法解析所请求的CAST类型:unsigned:unsigned [SELECT r FROM classname a WHERE a.tableB.ID = :ID ORDER BY CAST(col AS unsigned)]
英文:

I have a JPQL query in which I want to use cast operator in the order by clause. The query works in SQL but the application gives the shown error at runtime. How to use cast in orderby clause in JPQL query? Thanks in advance.

@Query("SELECT a FROM tableA a where a.tableB.ID = :ID ORDER BY cast(col as unsigned)")

Error

Caused by: org.hibernate.QueryException: Could not resolve requested type for CAST : unsigned : unsigned [SELECT r FROM classname a where a.tableB.ID = :ID ORDER BY CAST(col as unsigned)]

答案1

得分: 1

我认为不支持无符号类型。interger 可能是最接近的选项。

@Query("SELECT a FROM tableA a WHERE a.tableB.ID = :ID ORDER BY CAST(col AS INTEGER)")
英文:

I think unsigned is not supported. interger might be the closest.

@Query("SELECT a FROM tableA a where a.tableB.ID = :ID ORDER BY cast(col as integer)")

huangapple
  • 本文由 发表于 2020年10月24日 06:55:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/64508231.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定