如何使用JPQL获取数据库时间?
使用本机 SQL,我可以通过如下语句获取数据库时间:
SELECT CURRENT_TIMESTAMP
使用 JPQL,我得到相同的结果:
SELECT CURRENT_TIMESTAMP
FROM Customer c
WHERE c.id=1
有没有办法去掉最后两行?
谢谢,
with native SQL I get the database time with a statement like:
SELECT CURRENT_TIMESTAMP
with JPQL I get the same result with:
SELECT CURRENT_TIMESTAMP
FROM Customer c
WHERE c.id=1
Is there a way to get rid of the last two lines?
thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 JSR 220:Enterprise JavaBeans 3.0 规范:
因此,我已经很惊讶您可以编写第二种形式,该形式根据规范不正确,因此可能不可移植。
对我来说,“正确”的方法是创建一个具有 java.util.Date 类型的日期字段的类,并使用本机查询填充它。类似这样的事情:
然后:
According to the JSR 220: Enterprise JavaBeans 3.0 specifications:
So I'm already surprised you can write the 2nd form that is not correct per specification and might thus not be portable.
To me, the "right" way to do this would be to create a class with a date field of type
java.util.Date
and to populate it with a native query. Something like that:And then:
对于 java 8,可以将
Date
更新为Instant
,因此我将 @Pascal 的代码更改为:然后:
注意:
SELECT
更改为@Dominic 指出要让它发挥作用。但是,“SELECT CURRENT_TIMESTAMP AS DATE_VALUE FROM DUAL”
适用于 H2,但不适用于 postgres。“SELECT CURRENT_TIMESTAMP AS DATE_VALUE”
适用于“在我的系统上”(TM) 的 H2 和 postgresFor java 8, one would update
Date
toInstant
, so I changed @Pascal's code to:And then:
Note: The
SELECT
is changed as pointed by @Dominic to make it work. However,"SELECT CURRENT_TIMESTAMP AS DATE_VALUE FROM DUAL"
will work for H2 but not postgres."SELECT CURRENT_TIMESTAMP AS DATE_VALUE"
works for both H2 and postgres "on my system"(TM)