BigJ
BigJ

Reputation: 2032

JPA add seconds to date in query

I have a table called "Task" which has, among others, two columns:

I want to add the Wait_in_seconds value to the date in the query and compare it to the current date.

The query below doesn't have the right syntax, but it indicates what I want:

SELECT t
FROM Task t
WHERE (Date_due + Wait_in_seconds) < CURRENT_TIMESTAMP

Edit: KIMI explains that this is not possible with JPQL.

Edit 2: This native MySQL query does the trick:

SELECT *, DATE_ADD(task.date_due, INTERVAL task.wait_seconds SECOND) AS dueDate
FROM task                   
HAVING dueDate < NOW()
ORDER BY dueDate ASC

Upvotes: 3

Views: 7234

Answers (1)

Kimi
Kimi

Reputation: 6289

Standard JPQL does not support such operations on dates. You will have to either use a native query or do the computation on the Java side, for example with JodaTime which is a reference implementation of JSR-310.

Upvotes: 5

Related Questions