Metris Sovian
Metris Sovian

Reputation: 252

How to use ::date in java JDBC hibernate?

PG Admin

I have a query on SQL Editor in PGadmin:

select * from list_taxi where last_update::date = now()::date     

It show a good result, It show the taxi that online/update today. (I use ::date). Then I implement the query into Java using hibernate.

Java - JDBC Hibernate

I have a timestamp variable

Timestamp nowTime = new Timestamp(System.currentTimeMillis() );

I have query in Java DAO connected to Postgresql (JDBC - Hibernate)

ArrayList<Object[]> arrayList = 
(ArrayList) em.createQuery("SELECT lt FROM ListTaxi lt 
WHERE lt.lastUpdate::date = '" + nowTime + "'::date ")
.getResultList();

The result is error because of ::date

java.lang.IllegalArgumentException: 
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: 
: near line 1, column 75 [SELECT lt FROM model.ListTaxi lt WHERE 
lt.lastUpdate::date = lt.lastUpdate::date]

How to solve this?

Upvotes: 2

Views: 414

Answers (1)

Thilo
Thilo

Reputation: 262852

As a workaround you can do

WHERE lastUpdate > :startOfTodayTimestamp  -- set this to :00:00:00 today

If you have dates from the future or want to do this not just with today, you also need to add an upper bound.

Upvotes: 1

Related Questions