How do I query “older than three minutes” in JPA?

前端 未结 2 1797
情深已故
情深已故 2020-12-10 02:49

Is there a way to put a date range condition referencing the current time in a JPA query (that is compatible across databases)?

I can do

SELECT m FR         


        
2条回答
  •  有刺的猬
    2020-12-10 03:06

    JPA supports the CURRENT_TIMESTAMP and CURRENT_TIME functions.

    https://en.wikibooks.org/wiki/Java_Persistence/JPQL#Functions

    The JPA spec does not have date functions, but some JPA providers such as EclipseLink do.

    http://java-persistence-performance.blogspot.com/2012/05/jpql-vs-sql-have-both-with-eclipselink.html

    JPA 2.1 also defines the FUNCTION operator to call database functions.

    In EclipseLink I would try,

    SELECT m FROM Mail m WHERE m.sentAt < SQL("(sysdate - interval '3' minute)")
    

    or,

    SELECT m FROM Mail m WHERE m.sentAt < SQL("(? - interval '3' minute)", CURRENT_TIMESTAMP)
    

提交回复
热议问题