如何在 Oracle 数据库的 HQL 查询中使用当前日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/513317/
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
How do I use the current date in an HQL query with an Oracle database?
提问by bpapa
I'm trying to write this query using Hibernate 3 and Oracle 10.
我正在尝试使用 Hibernate 3 和 Oracle 10 编写此查询。
from Alert alert
where alert.expiration > current_date()
order by alert.priority, alert.updated, alert.name
It's creating SQL like this -
它正在像这样创建 SQL -
Hibernate: select alert0_.ANNOUNCEMENTS_ID as ANNOUNCE1_1_, alert0_.ANNOUNCEMENT
S_NAME as ANNOUNCE2_1_, alert0_.ANNOUNCEMENTS_PRIORITY as ANNOUNCE3_1_, alert0_.
ANNOUNCEMENTS_EXPIRATION as ANNOUNCE4_1_, alert0_.ANNOUNCEMENTS_UPDATE_DATE as A
NNOUNCE5_1_ from NYC311_ANNOUNCEMENTS alert0_ where (alert0_.ANNOUNCEMENTS_EXPIR
ATION>current_date()) order by alert0_.ANNOUNCEMENTS_PRIORITY , alert0_.ANNOUNC
EMENTS_UPDATE_DATE , alert0_.ANNOUNCEMENTS_NAME
I'm getting all of these wacky errors like "missing right parenthesis" when there is apparently perfectly balanced parenthesis.
当显然有完美平衡的括号时,我会收到所有这些古怪的错误,例如“缺少右括号”。
Why is Oracle freaking out at this? Is there a better way to write my HQL query?
为什么甲骨文对此感到害怕?有没有更好的方法来编写我的 HQL 查询?
回答by Michael Pralow
Shouldn't it be current_date
?
不应该current_date
吗?
Hibernate will translate it to the proper dialect.
Hibernate 会将其翻译成正确的方言。
I did not find a real "Hibernate will translate this to that" reference documentation, but the expression, in general, can be found in HQL Expressions for Hibernate 4.3.
我没有找到真正的“Hibernate will translate this to that”参考文档,但通常可以在HQL Expressions for Hibernate 4.3 中找到该 表达式。
Then there is the Java Persistence API 2.0 (JPA)specification which defines expressions for the Java Persistence query language (JPQL) and their meaning e.g. for current_date
:
然后是Java Persistence API 2.0 (JPA)规范,它定义了 Java Persistence 查询语言 (JPQL) 的表达式及其含义,例如current_date
:
4.6.17.2.3 Datetime Functions functions_returning_datetime:= CURRENT_DATE | CURRENT_TIME | CURRENT_TIMESTAMP The datetime functions return the value of current date, time, and timestamp on the database server.
4.6.17.2.3 日期时间函数functions_returning_datetime:= CURRENT_DATE | CURRENT_TIME | CURRENT_TIMESTAMP datetime 函数返回数据库服务器上的当前日期、时间和时间戳的值。
回答by Lost in Alabama
Is current_date()
a Hibernate function?
是current_date()
休眠功能吗?
I would use sysdate
instead. like this:
我会用sysdate
。像这样:
where alert.expiration > sysdate
Or to ignore time of day:
或者忽略一天中的时间:
where alert.expiration > trunc(sysdate)