简体   繁体   中英

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

I want to check weather validTill date is greater than today using JPQL. I know that I can achieve this by following.

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.

This is my code segment in the 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.

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_TIME - is evaluated to the current time (a java.sql.Time instance).

CURRENT_TIMESTAMP - is evaluated to the current timestamp, ie date and time (a java.sql.Timestamp instance).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM