Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 492
Faraj Farook Avatar asked Apr 02 '15 17:04

Faraj Farook


People also ask

What is the use of @query annotation?

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.

Which annotation is used for binding JPQL query?

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.

How do you write not equal to in JPQL?

When using Jpql, the correct operator for "not equal" is <> . So, update your code like this: return em.

Which is JPQL aggregate function?

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.


1 Answers

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).

like image 111
Faraj Farook Avatar answered Oct 14 '22 17:10

Faraj Farook