简体   繁体   English

带有Hibernate Criteria的复杂表达式(例如“ isnull(retryCount,0)<3”)

[英]Complicated expressions with Hibernate Criteria (like “isnull(retryCount,0)<3” )

I have some query like 我有一些查询

SELECT * FROM JobTable 
WHERE isnull(retryCount,0)<3 
AND updatedOn < dateadd(MI,-5,getdate())

How to convert it to Criteria api calls? 如何将其转换为Criteria API调用? Point to use Criteria is to allow refactoring if fields names will be changed. 使用准则如果要更改字段名称,则允许重构。

For simple things this is looks like 对于简单的事情,这看起来像

criteria.add(Restrictions.lt(JobTable.RETRYCOUNT_FULL, 3));

But what about my case? 但是我的案子呢?

criteria.add(Restrictions.lt(JobTable.UPDATEON_FULL, <???>);
criteria.add(Restrictions.lt( <someexpression(JobTable.RETRYCOUNT_FULL)> , 3));

I'm not sure of the correct name but hibernate has some standard functions which are mapped to the dialects functions. 我不确定名称是否正确,但是休眠有一些标准功能,它们映射到方言功能。 They can be used like this 它们可以像这样使用

Projections.sqlFunction("dateadd", Projections.property("property"), Hibernate.dateTime);

Hibernate Criteria API has Restrictions.sqlRestriction functions. Hibernate Criteria API具有Restrictions.sqlRestriction函数。 In these functions you write your SQL the way you like and Hibernate embeds it in to your SQL. 在这些函数中,您可以按自己喜欢的方式编写SQL,然后Hibernate将其嵌入到SQL中。

criteria.add(Restrictions.sqlRestriction("isnull({alias}" + JobTable.RETRYCOUNT_FULL + ", 0) < ?", 3, new IntegerType());  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM