开发者

With Hibernate/JPA query inHQL/JQL only how do I set where clause to last x days?

开发者 https://www.devze.com 2023-02-08 08:08 出处:网络
I am looking for the HQL equivalent of converting x amounts days from current times开发者_JAVA技巧tamp to a queriable value.

I am looking for the HQL equivalent of converting x amounts days from current times开发者_JAVA技巧tamp to a queriable value.

So like this sudo-HQL : from Newspaper as newspaper where newspaper.published < current_timestamp - days(:daysparam)

And then daysparam is injected as query parameter. And published is date field.

Is this in anyway doable in HQL only, without writing your own hibernate dialect or using criteria in actual code? It seems such as standard feature to not be supported by plain HQL seems strange.

I am using Spring batch's HibernatePagingItemReader which is xml only, so I wanted to avoid the yakshaving of extending that class or creating my own custom dialect etc.

Similar question seems to only suggest calendar critera or new dialect:

Performing Date/Time Math In HQL?

How to perform date operations in hibernate HQL


Something doesn't look quite right: you said that published is a date field (in the database, I suppose). Then, you are using current_timestamp minus an integer value to compare with the date field. As result, you are not getting the timestamp for the date in the parameter, you are just getting current_timestamp - 2, which I don't believe represents "two days ago" ;-) If you have used current_date, I guess it might work.

from Newspaper as newspaper where newspaper.published < current_date - :daysparam

But still, I'd prefer to leave this calculation to the Java side, so that the query would be:

from Newspaper as newspaper where newspaper.published < :start_date

This won't work only if you are not using UTC in your servers (which you should).

0

精彩评论

暂无评论...
验证码 换一张
取 消