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.
The @Query annotation can only be used to annotate repository interface methods. The call of the annotated methods will trigger the execution of the statement found in it, and their usage is pretty straightforward. The @Query annotation supports both native SQL and JPQL.
In order to define SQL to execute for a Spring Data repository method, we can annotate the method with the @Query annotation — its value attribute contains the JPQL or SQL to execute. The @Query annotation takes precedence over named queries, which are annotated with @NamedQuery or defined in an orm. xml file.
When using Jpql, the correct operator for "not equal" is <> . So, update your code like this: return em.
JPQL supports the five aggregate functions of SQL: COUNT - returns a long value representing the number of elements. SUM - returns the sum of numeric values. AVG - returns the average of numeric values as a double value.
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, i.e. date and time
(a java.sql.Timestamp instance).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With