Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle + JPA - querying with INTERVAL

Tags:

select

oracle

jpa

I have a Timestamp column in my DB table, LASTUPDATED, which I poll to decide if a row should be updated or not. If the record was not updated in the last 10 minutes, I update it. I want to delegate the date handling to the DB, but none of the following works:

This one says "enexpected token: : near[...]"

    Query query = entityManager.createQuery("SELECT x FROM MyEntity x WHERE x.lastUpdated < SYSTIMESTAMP - INTERVAL :olderThen MINUTE");
    query.setParameter("olderThen", 10);
    list = query.getResultList();

This one says "unexpected token: '10' near[...]

    Query query = entityManager.createQuery("SELECT x FROM MyEntity x WHERE x.lastUpdated < SYSTIMESTAMP - INTERVAL '10' MINUTE");
    list = query.getResultList();

And this one says unexpected token: '?' near[...]

    Query query = entityManager.createQuery("SELECT x FROM MyEntity x WHERE x.lastUpdated < SYSTIMESTAMP - INTERVAL ?1 MINUTE");
    query.setParameter(1, 10);
    list = query.getResultList();

Can I use this INTERVAL keyword with JPA by the way? The expression is correct syntactically, I tested it with console.

Thanks for the help Guys, Gergely

like image 680
Gergely Kovács Avatar asked Jul 18 '13 10:07

Gergely Kovács


People also ask

What is the difference between JPA and JPQL?

The main difference between JPQL and SQL lies in that the former deals with JPA entities, while the latter deals directly with relational data.


1 Answers

JPA and Hibernate does not sopport interval, finally I found it...

Date arithmetic is also supported, albeit in a more limited fashion. This is due partially to differences in database support and partially to the lack of support for INTERVAL definition in the query language itself. Source: http://docs.jboss.org/hibernate/orm/4.1/devguide/en-US/html/ch11.html

Only the BETWEEN keyword can be used. Take care

Btw the correct expression was:

     select x from MyEntity x where cast((systimestamp - (1/24/60) * 10) as timestamp) between lastUpdated and systimestamp
like image 93
Gergely Kovács Avatar answered Sep 19 '22 22:09

Gergely Kovács