使用hibernate查询查找其中列为varchar类型的max(列)

时间:2014-08-18 12:15:12

标签: java hibernate

您好我有一个varchar类型的列,例如:

1
2
3
4
5

我想获得最大值,所以我尝试了以下查询

select max(siAnswersId) from < table>

但是这给了我错误的输出所以我尝试使用

MAX(CAST(siAnswersId AS INTEGER))当我在sql developer

中运行时它运行正常

但是当我在hibernate中构建查询时

select MAX(CAST(SI_ANSWERS_ID AS INTEGER)) from < table> 

它给我以下错误

No data type for node: org.hibernate.hql.internal.ast.tree.AggregateNode 
<| \-[AGGREGATE] AggregateNode: 'MAX'<|    \-[METHOD_CALL] MethodNode: '('<|
       +-[METHOD_NAME] IdentNode: 'CAST' {originalText=CAST}<|
       \-[EXPR_LIST] SqlNode: 'exprList'<|
          +-[DOT] DotNode: 'serviceins0_.SI_ANSWERS_ID'
{propertyName=siAnswersId,dereferenceType=PRIMITIVE,getPropertyPath=siAnswersId,path
{synthetic-alias}.siAnswersId,tableAlias=serviceins0_,
className=com.hp.api.management.nfv.questionnair.entities.ServiceInstanceAnswersEntity,
classAlias=s}<|
      |  +-[IDENT] IdentNode: '{synthetic-alias}' {originalText={synthetic-alias}}<|
      |  \-[IDENT] IdentNode: 'siAnswersId' {originalText=siAnswersId}<|
      \-[IDENT] IdentNode: 'INT' {originalText=INT}<|

2 个答案:

答案 0 :(得分:2)

实际上&#34; int&#34;

必须小写
MAX(CAST(siAnswersId AS int)) 

它不起作用是INT,整数或整数。该消息实际上表示&#34;没有数据类型&#34;因为它没有将这些识别为有效的数据类型。

答案 1 :(得分:0)

MAX(CAST(siAnswersId AS INT))应该这样做。 INT代替INTEGER