Search code examples
jpajpa-2.0jpqltemporal

Taking the difference of temporal fields in JPQL


I have an entity Event which has fields startDate and endDate. I'd like to select only those events that have at most x days left to the their endDate. I want to do this in JPQL and with only one query. How can I do this?


Solution

  • JPA does not provide any standard date/time functions. You can use a native SQL query using SQL EXTRACT,

    Or, if you are using EclipseLink you can use the FUNC JPQL operator to call a database specific function, or use EXTRACT if using EclipseLink 2.4,

    See,

    http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/JPQL#Functions