to_char函数抛出Hibernate异常

时间:2015-10-02 16:02:26

标签: sql hibernate grails groovy gorm

我正在尝试在grails域对象上执行sql语句。在db(h2)上直接执行时工作正常。

Call.executeQuery "select to_char(date,'DD') from Call"

通过休眠我得到:

  

节点没有数据类型:org.hibernate.hql.internal.ast.tree.MethodNode    - [METHOD_CALL] MethodNode:'('       + - [METHOD_NAME] IdentNode:'to_char'{originalText = to_char}        - [EXPR_LIST] SqlNode:'exprList'          + - [DOT] DotNode:'call0_.date'{propertyName = date,dereferenceType = PRIMITIVE,getPropertyPath = date,path = {synthetic-alias} .date,tableAlias = call0_,className = com.olamagic.Call,classAlias = null }          | + - [IDENT] IdentNode:'{synthetic-alias}'{originalText = {synthetic-alias}}          | - [IDENT] IdentNode:'date'{originalText = date}           - [QUOTED_STRING] LiteralNode:''DD''

1 个答案:

答案 0 :(得分:1)

当您直接在H2上执行SQL语句时,您正在执行H2的SQL。 GormEntity.executeQuery(String sql)方法执行HQL,而不是SQL。

HQL没有to_char()功能。因此,获得相同的结果更为复杂:

select case when day(date) > 9 then cast(day(date) as text) else concat('0', day(date)) end from Call

如果您不介意将某一天作为整数,那么查询将简化为:

select day(date) from Call