to_char 函数在 Hibernate 中抛出异常

to_char function throws exception with Hibernate

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

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

通过休眠我得到:

No data type for node: 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''

当您直接在 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