如何查询“超过三分钟”在JPA? [英] How do I query "older than three minutes" in JPA?

查看:125
本文介绍了如何查询“超过三分钟”在JPA?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在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.org/wiki/Java_Persistence/JPQL#Functions

JPA规范没有日期功能,但是一些JPA提供程序(如EclipseLink) 。

The JPA spec does not have date functions, but some JPA providers such as EclipseLink do.

http://java-persistence-performance.blogspot.com/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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆