如何查询“超过三分钟”在JPA?

编程入门 行业动态 更新时间:2024-10-23 19:18:09
本文介绍了如何查询“超过三分钟”在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/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?

本文发布于:2023-11-26 12:58:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1633970.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:JPA

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!