问题描述
我有一个实体Event
,它具有字段startDate
和endDate
.我只想选择那些距x
天最多剩下x
天的事件.我想用一个查询在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?
推荐答案
JPA不提供任何标准的日期/时间函数.您可以通过SQL EXTRACT使用本机SQL查询,
JPA does not provide any standard date/time functions. You can use a native SQL query using SQL EXTRACT,
或者,如果使用的是EclipseLink,则可以使用FUNC JPQL运算符来调用数据库特定的函数,如果使用的是EclipseLink 2.4,则可以使用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,
看到,
http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/JPQL#Functions
这篇关于取得JPQL中时域的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!