我想在grails域對象上執行sql語句。在db(h2)上直接執行時工作正常。to_char函數拋出異常與休眠
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: '日期'{originalText =日期} - [QUOTED_STRING] LiteralNode: '' DD ''
要是我需要更復雜的格式,讓我們說dd-mm-yyy。反正有強迫執行語句再次數據庫而寧? – Macchiatow
HQL具有日,月,年等功能,但不具有日期格式功能。您可以使用Hibernate會話執行SQL查詢。就像這樣:'Call.withSession {session - > def result = session.createSqlQuery('select ...')。list()}'你也可以使用HQL來返回date對象並在Groovy中格式化:'def dates = Call.executeQuery(「從呼叫中選擇日期」)* .format('dd-mm-yyyy')'。 http://docs.jboss.org/hibernate/orm/4.2/devguide/en-US/html/ch11.html#ql-exp-functions –
哦酷。我不知道與本機SQL技巧。謝謝。 – Macchiatow