java JPQL检查@Query注释中大于今天的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29418884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JPQL Check greater than less than date today in @Query annotation
提问by Faraj Farook
I want to check weather validTill
date is greater than today
using JPQL. I know that I can achieve this by following.
我想检查天气validTill
日期是否大于today
使用 JPQL。我知道我可以通过以下方式实现这一目标。
Query q = em.createQuery("select e from MyEntity e where e.validTill > :today ");
and pass the :today
parameter. But this is not I wanted.I want to do this using @Query
annotation in the CrudRepository
in Spring.
并传递:today
参数。但这不是我想要的。我想在 Spring 中使用@Query
注释来做到这一点CrudRepository
。
This is my code segment in the CrudRepository
这是我的代码段 CrudRepository
@Query("SELECT e FROM MyEntity e WHERE e.validFrom < TODAY")
Iterable<MyEntity> findAllValid();
I don't know what I should put at the place TODAY
to get the today's date. Please help me.
我不知道我应该在那个地方放什么TODAY
才能得到今天的日期。请帮我。
回答by Faraj Farook
I found it. it's like this..
我找到了。就像这样..
@Query("SELECT e FROM MyEntity e WHERE e.validFrom < CURRENT_DATE")
Iterable<MyEntity> findAllValid();
CURRENT_DATE
- is evaluated to the current date (a java.sql.Date instance).
CURRENT_DATE
- 计算为当前日期(一个 java.sql.Date 实例)。
CURRENT_TIME
- is evaluated to the current time (a java.sql.Time instance).
CURRENT_TIME
- 计算为当前时间(一个 java.sql.Time 实例)。
CURRENT_TIMESTAMP
- is evaluated to the current timestamp, i.e. date and time
(a java.sql.Timestamp instance).
CURRENT_TIMESTAMP
- 评估为当前时间戳,即日期和时间(java.sql.Timestamp 实例)。