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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:14:08  来源:igfitidea点击:

JPQL Check greater than less than date today in @Query annotation

javaspringspring-bootspring-datajpql

提问by Faraj Farook

I want to check weather validTilldate is greater than todayusing 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 :todayparameter. But this is not I wanted.I want to do this using @Queryannotation in the CrudRepositoryin 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 TODAYto 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 实例)。