How to compare timestamp to date without time in JPQL?

I would like to get objects from create_date = :my_date

where create_date

is in format 2014-12-02 14:49:15.029

and my_date

- in format 2014-12-02

in JPA.

Is there a way to compare timestamp column with Date object (yyyy-MM-dd format) in JPQL using NamedQuery

or using my own query?

+4


source to share


2 answers


You can use NamedQuery, you need to manipulate the @ Temporal annotation and TemporalType enum correctly .

So, if your object property is a date without a time part, you should annotate it like @Temporal(TemporalType.DATE)

, then when supplying a parameter to your named query, you would use one of the two available signatures

setParameter(String name, java.util.Date value, TemporalType temporalType)
setParameter(String name, java.util.Calendar value, TemporalType temporalType)

      



so your request will have the same part as:

query.setParameter("my_date", myDate, TemporalType.DATE)

      

+1


source


If you are using jpql try this

use date (timestamp_column)



For example: @Query (select te from Testentity te where date (te.createdTimestamp)> = CURRENT_DATE) public list getTestDate ()

0


source







All Articles