本文介绍了NHibernate和MySql关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么Nibernate HQL无法处理以下查询:
Why Nibernate HQL can not handle the following query:
from Deal D where (D.ApprovalDate + INTERVAL 1 Year) < current_timestamp() < (D.RenewalDate + INTERVAL -1 Year)
知道INTERVAL和YEAR是MySQL中的关键字,因此这是在Hql中混合Sql的一种方式(除非Hql可以像这样处理日期函数,但我不知道).该方言为 MySQLDialect
knowing that INTERVAL and YEAR are keywords in MySQL, so this is kind of mixing Sql within Hql (unless Hql can handle date functions like so and I don't know) . The dialect is MySQLDialect
执行此查询非常有效
SELECT '2005-01-01' + INTERVAL 1 Year;
推荐答案
您可以使用条件查询在查询中直接传递Sql.像这样
You can pass the Sql directly in the query using Criteria Query. Something like this
Session.CreateCriteria<Deal>()
.Add(Restrictions.Sql(D.ApprovalDate + " INTERVAL 1 Year < current_timestamp() < " + D.RenewalDate + " INTERVAL -1 Year")
Restrictions.Sql会将您提供的所有内容直接以sql的形式传递给数据库.
The Restrictions.Sql will pass whatever you give it directly to the database as sql.
这篇关于NHibernate和MySql关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!