如何在Nhibernate中使用MySql date_add?

时间:2010-04-17 12:14:12

标签: mysql nhibernate hql dateadd

这真是困惑了几个小时,我在互联网上搜索,但没有找到解决方案。有人可以指出问题所在......谢谢! 我创建了自己的方言类

public class MySQLDialectExtended : MySQLDialect
{
    public MySQLDialectExtended()
    {
        RegisterFunction("date_add_interval", new SQLFunctionTemplate(NHibernateUtil.Date, "date_add(?1, INTERVAL ?2 ?3)"));            
    }
}

然后我尝试按如下方式使用它:

query.Append(
   " ( date_add_interval(D.ApprovalDate, 1, YEAR) < current_timestamp() <  date_add_interval(D.RenewalDate, -1, YEAR) )");

失败并出现以下异常:

NHibernate.Hql.Ast.ANTLR.QuerySyntaxException : Exception of type 'Antlr.Runtime.NoViableAltException' was thrown. near line 1, column 677

其中列号位于第一个'YEAR'字的末尾。

编辑: 这是我的配置

    <property name="dialect">MyCompanyName.MySQLDialectExtended, MyCompanyName</property>
    <property name="hbm2ddl.keywords">none</property>

1 个答案:

答案 0 :(得分:0)

你可以发布整个NHibernate查询吗?

更新:嗯,查询显然是格式错误和错误的:

Select distinct D from MBIgnition.Core.Domain.Model.Deal D where 1=1 and 
( (D.MortgageStatus = 30 ) or 
  (D.MortgageStatus = 35 ) or 
  (D.MortgageStatus = 40 ) or 
  (D.MortgageStatus = 45 ) or 
  (D.MortgageStatus = 55 ) or 
  (D.MortgageStatus = 50 ) ) and 
  // next line is erroneous as the first AND operator does not have a lefthand side operand
(( and ( date_add_interval(D.ApprovalDate, 1, YEAR) < current_timestamp() < date_add_interval(D.RenewalDate, -1, YEAR) ) ) )

正如您所看到的,代码中有一个AND运算符,没有任何左侧参数。你的HQL应该有问题。再次仔细检查,如果您无法查明错误,那么在此处发布HQL或标准构建代码将非常有用。