如何在 Nhibernate 中使用 MySql date_add?
这确实让我困惑了好几个小时,我在互联网上进行了搜索,但没有得到有效的解决方案。有人能指出问题出在哪里吗...谢谢! 我创建了自己的方言类
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你能发布整个 NHibernate 查询吗?
更新:嗯,查询显然是格式错误且错误的:
如您所见,代码中有一个
AND
运算符,没有任何左侧参数。你的HQL应该有问题。再次仔细检查,如果您无法查明错误,那么在此处发布 HQL 或标准构建代码将会很有用。Can you post the whole NHibernate query?
UPDATE: Well, the query is obviously malformed and erroneous:
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.