我想在EQL里面用mysql的函数 date_add(date,INTERVAL expr unit)
在网上查了下,不支持,根据网上介绍的,我写个MySQL5Dialect的子类public class MysqlDialect extends MySQL5Dialect { public MysqlDialect() {
super();
registerFunction("DATE_ADD_INTERVAL",
new SQLFunctionTemplate(Hibernate.DATE, "DATE_ADD(?1, INTERVAL ?2 ?3)")); 
}

}sql里面这样用的函数
DATE_ADD_INTERVAL(c.releaseDate,INTERVAL c.reviewPeriod YEAR) //发布日期加上复审周期 单位是按年Query query = em.createQuery(sql) 和 em.createNativeQuery(sql,XXX.class ) 用了都不行还说得配置 方言,是不是在 persistence.xml 这个文件里配置啊
<property name="hibernate.dialect" value="com.tsd.utils.MysqlDialect"/> //这是我写的子类还是不行的。

解决方案 »

  1.   

    用 createQuery 出现这个错误:
    13:43:25,296 ERROR [PARSER] line 1:573: unexpected token: c
    13:43:25,296 ERROR [PARSER] line 1:588: unexpected token: YEAR
    13:43:25,296 WARN  [HqlParser] processEqualityExpression() : No expression to process!
    13:43:25,296 ERROR [PARSER] line 1:714: unexpected token: MONTH
    13:43:25,296 WARN  [HqlParser] processEqualityExpression() : No expression to process!用createNativeQuery 出现:
    13:58:03,359 WARN  [JDBCExceptionReporter] SQL Error: 1064, SQLState: 42000
    13:58:03,359 ERROR [JDBCExceptionReporter] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IN( p.criterion) c WHERE ( p.year = 2012  OR  p.year = 2013 )  AND p.system ' at line 1
    13:58:03,843 ERROR [runtime] Method public java.lang.String org.hibernate.exception.NestableRuntimeException.getMessage(int) threw an exception when invoked on org.hibernate.exception.SQLGrammarException: could not execute query
    Method public java.lang.String org.hibernate.exception.NestableRuntimeException.getMessage(int) threw an exception when invoked on org.hibernate.exception.SQLGrammarException: could not execute query
    The problematic instruction:
    ----------
    ==> ${msg[0]} [on line 68, column 29 in org/apache/struts2/dispatcher/error.ftl]
    ----------Java backtrace for programmers:
    ----------
    freeer.template.TemplateModelException: Method public java.lang.String org.hibernate.exception.NestableRuntimeException.getMessage(int) threw an exception when invoked on org.hibernate.exception.SQLGrammarException: could not execute query