Java 在 JPA 查询中使用 CURRENT_DATE 的示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1637323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Example of using CURRENT_DATE in JPA query
提问by javydreamercsw
Can anyone point me to an example on how to use CURRENT_DATE
in a JPA query?
谁能指出我如何CURRENT_DATE
在 JPA 查询中使用的示例?
CURRENT_DATE
is specified in JPA but I haven't been able to make it work. I always get the unexpected token [CURRENT_DATE]
exception. Since it is specified in JPA all providers should comply with it right?
CURRENT_DATE
在 JPA 中指定,但我无法使其工作。我总是得到unexpected token [CURRENT_DATE]
例外。既然它在 JPA 中指定,所有提供者都应该遵守它,对吗?
I'm using EclipseLink 2.0 BTW.
我正在使用 EclipseLink 2.0 BTW。
采纳答案by McDowell
It can be used like so:
它可以像这样使用:
Query query = manager
.createQuery("SELECT c FROM CITIES c WHERE c.founded = CURRENT_DATE");
for (Object city : query.getResultList()) {
System.out.println(city);
}
...where foundedis a temporal type:
...其中成立的是一个时间类型:
@Column(name = "FOUNDED")
@Temporal(TemporalType.DATE)
private Date founded = new Date();
Not a great example, but you get the idea. I'm using Eclipselink 1.1.2
不是一个很好的例子,但你明白了。我正在使用 Eclipselink 1.1.2
回答by jitter
If you are using the Expression Frameworkthere is the Expression currentDateDate()
method on the Expression
(org.eclipse.persistence.expressions.Expression
) object.
如果您使用的是表达式框架,则Expression currentDateDate()
有Expression
( org.eclipse.persistence.expressions.Expression
) 对象上的方法。
回答by javydreamercsw
The answer to this question was retrieving the values via JPA and then do the math in plain Java.
这个问题的答案是通过 JPA 检索值,然后用普通的 Java 进行数学运算。