NHibernate and MySql Keywords

2019-08-31 03:39发布

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) 

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

Its perfectly valid to execute this query

  SELECT '2005-01-01' + INTERVAL 1 Year;

1条回答
疯言疯语
2楼-- · 2019-08-31 04:26

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

The Restrictions.Sql will pass whatever you give it directly to the database as sql.

查看更多
登录 后发表回答