如何在 Nhibernate 中使用 MySql date_add?

发布于 2024-08-29 09:40:35 字数 955 浏览 7 评论 0原文

这确实让我困惑了好几个小时,我在互联网上进行了搜索,但没有得到有效的解决方案。有人能指出问题出在哪里吗...谢谢! 我创建了自己的方言类

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>

This really puzzled for hours, I searched all over the internet, but got no working solution. Can someone point where the problem is ... thanks !
I created my own dialect class

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

Then I try to use it as follows:

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

It fails with following exception:

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

where the column number is at the end of the first 'YEAR' word.

Edit:
here is my configuration

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

说不完的你爱 2024-09-05 09:40:35

你能发布整个 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 或标准构建代码将会很有用。

Can you post the whole NHibernate query?

UPDATE: Well, the query is obviously malformed and erroneous:

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

As you can see, there's an AND operator in your code without any lefthand-side arguments. There should be something wrong with your HQL. Double check it again and if you couldn't pinpoint the error it will be useful to post the HQL or the criteria building code here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文