Question

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?

Was it helpful?

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top