简体   繁体   English

日期差异(天)限制休眠

[英]Date difference in days Restriction Hibernate

I am using Hibernate and Criteria API to write my database quires. 我正在使用Hibernate和Criteria API编写数据库查询。 What I need to do is I need two dates difference in days and compare that days with with specific number. 我需要做的是,我需要两个日期相差几天,然后将这些天与特定数字进行比较。

Eg Commonly written criteria restriction as: 例如,通用书面条件限制如下:

criteria.add(Restrictions.eq("somProperty", someValue));

What I needs is: 我需要的是:

criteria.add(Restrictions.ge("dateProperty1 - dateProperty2", 15));

Means date difference between two dates is greater than or equal 15 days. 表示两个日期之间的日期差大于或等于15天。

I don't see how to achieve this. 我看不出如何实现这一目标。 And yes I did lots of Google to find out the possible solution but didn't get proper material what I need. 是的,我做了很多Google来寻找可能的解决方案,但没有得到我需要的适当材料。

If you check the documentation of the Restrictions class, you will see that: 如果查看Restrictions类的文档,您将看到:

  • most functions operate on a "property vs value" pair (just like in your first example) 大多数功能都在“属性与价值”对上运行(就像您的第一个示例一样)
  • the remaining comparator functions operate on "property vs other property" pair 其余比较器功能在“属性与其他属性”对上运行

But no customized function is available for your case. 但是没有针对您的情况的自定义功能。 So what option you have is using the sqlRestriction , which can be used to express this condition in a native form of your DBMS. 因此,您拥有的选择是使用sqlRestriction ,它可以用于以DBMS的本机形式表示这种情况。 That would be a different, but much easier problem, altough clearly not as elegant as your original idea. 那将是一个不同但容易得多的问题,尽管显然不如您最初的想法那么优雅。

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

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