简体   繁体   中英

to_char function throws exception with Hibernate

I'm trying to execute sql statement on a grails domain object. Is works fine when executed on db (h2) directly.

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

Via hibernate I get:

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''

When you executed the SQL statement on H2 directly you were executing H2's SQL. The GormEntity.executeQuery(String sql) method executes HQL, not SQL.

HQL doesn't have a to_char() function. So getting the equivalent result is more involved:

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

If you don't mind getting the day as an Integer, then the query simplifies to:

select day(date) from Call

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM