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?
source to share
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)
source to share