有没有办法在JPA查询中引用当前时间的日期范围条件(跨数据库兼容)?
Is there a way to put a date range condition referencing the current time in a JPA query (that is compatible across databases)?
我可以做
SELECT m FROM Mail m WHERE m.sentAt < :date但是我想做,而不必绑定一个参数(这样可以配置作为命名查询库的一部分,日期计算逻辑不必输入调用代码)。
but I want to do it without having to bind a parameter (so that this can be configured as part of the named query repository and the date calculation logic does not have to enter the calling code).
所以而不是:date 我需要currentTime减3分钟作为硬码文字。
So instead of :date I need "currentTime minus 3 minutes" as a hard-code literal.
推荐答案JPA支持CURRENT_TIMESTAMP和CURRENT_TIME函数。
JPA supports the CURRENT_TIMESTAMP and CURRENT_TIME functions.
https:/ /en.wikibooks/wiki/Java_Persistence/JPQL#Functions
JPA规范没有日期功能,但是一些JPA提供程序(如EclipseLink) 。
The JPA spec does not have date functions, but some JPA providers such as EclipseLink do.
java-persistence-performance.blogspot/2012/05/jpql-vs-sql-have-both-with-eclipselink.html
JPA 2.1还定义了调用数据库函数的FUNCTION运算符。
JPA 2.1 also defines the FUNCTION operator to call database functions.
在EclipseLink中,我会尝试,
In EclipseLink I would try,
SELECT m FROM Mail m WHERE m.sentAt < SQL("(sysdate - interval '3' minute)")或
SELECT m FROM Mail m WHERE m.sentAt < SQL("(? - interval '3' minute)", CURRENT_TIMESTAMP)更多推荐
如何查询“超过三分钟”在JPA?
发布评论