Lukasz Puchala
Lukasz Puchala

Reputation: 476

How to compare timestamp with date without time in JPQL?

I'd like to get objects with create_date = :my_date where create_date is in 2014-12-02 14:49:15.029 format and my_date is in 2014-12-02 format in JPA.

Is there any way to compare timestamp column with Date object (yyyy-MM-dd format) in JPQL using NamedQuery or should I use native query?

Upvotes: 10

Views: 17852

Answers (2)

Arun Nair
Arun Nair

Reputation: 106

If you are using jpql try to use

date(timestamp_column) 

eg :

@Query(select te from Testentity te where date(te.createdTimestamp) >= CURRENT_DATE)
 public List<Testentity> getTestDate() 

Upvotes: 8

Master Slave
Master Slave

Reputation: 28519

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

So if your object property is a Date without a time portion, you would annotate it as @Temporal(TemporalType.DATE), further on 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 query will have a part like:

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

Upvotes: 1

Related Questions