使用 HQL 的 varchar 列的 MAX
我正在尝试使用 hibernate 查询语言查找 mySQL DB 中 varchar 列的最大值。
这是我正在尝试执行的查询,这在本机 SQL 中运行良好:
select max(cast(rs.marks as unsigned)) from Results rs;
其中 column:marks 是 varchar。
假设如果列标记(所有值都保存为文本)具有以下行:
65
75
82
41
那么查询应该返回唯一的结果“82”。 但是当我在 HQL 中编写相同的内容时,它不起作用。谁能帮我告诉我如何在 Hibernate 中写这个?
控制台说:
java.lang.IllegalStateException: No data type for node:org.hibernate.hql.ast.tree.AggregateNode [AGGREGATE] AggregateNode: 'max' [METHOD_CALL] MethodNode: '(' [METHOD_NAME] IdentNode: 'cast' {originalText=cast} [EXPR_LIST] SqlNode: 'exprList' [DOT] DotNode: 'results0_.marks_' {propertyName=marks,dereferenceType=4,propertyPath=marks,path={synthetic-alias}.marks,tableAlias=results0_,className=com.Results,classAlias=rs} [IDENT] IdentNode: '{synthetic-alias}' {originalText={synthetic-alias}} [IDENT] IdentNode: 'marks' {originalText=marks} [IDENT] IdentNode: 'unsigned' {originalText=unsigned}
I am trying to find the maximum value of a varchar column in mySQL DB using hibernate query language.
This is the query i am trying to do, and this works fine in native SQL:
select max(cast(rs.marks as unsigned)) from Results rs;
where column:marks is a varchar.
Suppose if column marks (where all the values are saved as texts) has the following rows:
65
75
82
41
Then the query should return me a unique result "82".
But when i wrote the same in HQL, it doesn't work. Can anyone please help me out by telling me how to write this in Hibernate?
Console says:
java.lang.IllegalStateException: No data type for node:org.hibernate.hql.ast.tree.AggregateNode [AGGREGATE] AggregateNode: 'max' [METHOD_CALL] MethodNode: '(' [METHOD_NAME] IdentNode: 'cast' {originalText=cast} [EXPR_LIST] SqlNode: 'exprList' [DOT] DotNode: 'results0_.marks_' {propertyName=marks,dereferenceType=4,propertyPath=marks,path={synthetic-alias}.marks,tableAlias=results0_,className=com.Results,classAlias=rs} [IDENT] IdentNode: '{synthetic-alias}' {originalText={synthetic-alias}} [IDENT] IdentNode: 'marks' {originalText=marks} [IDENT] IdentNode: 'unsigned' {originalText=unsigned}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
遇到这个问题并决定回答,尽管它已经很老了。
首先这足以获得最大值
其次HSQL中没有
UNSIGNED
关键字。因此如果结果值需要转换为INT
Came across this question and decided to answer although it's very old.
First of all this will suffice to get MAX value
Secondly there is no
UNSIGNED
keyword in HSQL. Therefore if the result value needs to be converted toINT