Performing Date/Time Math In HQL?
datetime, hibernate, hql
Solution
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.
Problem
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 query example: ``` FROM RandomThing WHERE randomTime IS NOT NULL AND randomTime >= current_timestamp() AND randomTime <= (current_timestamp() + :timeToAdd) ``` 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. 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.