在HQL中执行日期/时间数学? [英] Performing Date/Time Math In HQL?

查看:166
本文介绍了在HQL中执行日期/时间数学?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找如何在HQL查询中执行日期/时间数学。具体来说,如何从 current_timestamp()函数的结果中添加或减去(x)的时间量?或者我必须放入SQL中,希望无论数据库运行是否支持?

I'm looking how to perform date/time math within an HQL query. Specifically, how do I add or subtract (x) amount of time from the result of the current_timestamp() function? Or do I have to drop into SQL for this and hope that whatever database is being run supports it?

HQL查询示例:

FROM RandomThing
WHERE randomTime IS NOT NULL AND
      randomTime >= current_timestamp() AND
      randomTime <= (current_timestamp() + :timeToAdd)

我可以将:timeToSubtract参数定义为任何特定的单位,小时是不受欢迎的,秒数是最理想的。

I can define the :timeToSubtract parameter to be any particular unit, though anything bigger than hours would be undesirable, and seconds would be most desirable.

澄清:我意识到这可以很容易地在查询之外完成。但是出于哲学原因,让我们说使用数据库服务器的时间,而不是查询系统的时间很重要。实际示例:我在最后(x)个时间内查询所有条目的自动时间戳。由于时间戳是由数据库系统进行的,所以重要的是还要使用数据库的当前时间。

CLARIFICATION: I realize this can be easily done outside of the query. But for philosophical reasons, let's say it's important to use the database server's time, rather than the querying system's time. Practical example: I'm querying an automatic timestamp for all entries made within the last (x) amount of time. Since the timestamp is made by the database system, it is important to also use the database's current time.

推荐答案

如果您需要数据库服务器的时间,您可以先做一个简单的hql查询以获取时间戳,然后在java中计算maxTimestamp,并将获取的时间戳和计算的maxTimeStamp传递给像ccclark之类的查询。

If you need the database server's time, you could first do a simple hql query to get the timestamp and then calculate the maxTimestamp in java and pass the fetched timestamp and the calculated maxTimeStamp to a query like the one of ccclark.

这篇关于在HQL中执行日期/时间数学?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆