在JPA查询中比较当前日期时间 [英] Compare current Date time in JPA query

查看:1541
本文介绍了在JPA查询中比较当前日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数据库中的日期与JPA查询中的当前dateTime进行比较:

I want to compare the date in database with current dateTime in JPA query :

captureLimitDate< currentDateTime

captureLimitDate < currentDateTime

我的要求如下:

database.captureLimitDate:2012年4月7日19:03:00 currentDateTime:2012年4月7日20:03:00

database.captureLimitDate : 04/07/2012 19:03:00 currentDateTime : 04/07/2012 20:03:00

我的JPAQuery是这样的:

My JPAQuery is this :

SELECT o FROM Operation o"
+ " WHERE ( o.merchantId =:merchantId ) AND "
+ "(o.captureLimitDate < currentDateTime ) ";

Operation类的captureLimitDate为java.util.Date

And Operation class has captureLimitDate as java.util.Date

    @Generated(value = "XA", comments = "0,_8BedAMXZEeGHf_Dj4YaPyg")
     private Date captureLimitDate;

我想比较当前日期和时间.上面的查询将工作. ??

I want to compare both current date and time . will the above query works. ??

推荐答案

在JPQL查询中,CURRENT_TIMESTAMP必须用于引用当前日期和时间:

CURRENT_TIMESTAMP must be used to refere to the current date and time in a JPQL query:

select o from Operation o
where o.merchantId = :merchantId
and o.captureLimitDate < CURRENT_TIMESTAMP

如果当前日期和时间实际上是来自用户输入的日期(因此不是当前日期和时间,则可以像对merchantId那样进行操作:

If the current date and time is in fact a date coming from user input (and which is thus not the current date and time, then you do it like you do for the merchantId:

select o from Operation o
where o.merchantId = :merchantId
and o.captureLimitDate < :maxDateTime

然后您使用设置参数

query.setParameter("maxDateTime", maxDateTime, TemporalType.TIMESTAMP);

这篇关于在JPA查询中比较当前日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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