I am trying to filter records in HQL with timestamp dates for some specific time range. For example, get all items created between 1pm and 3pm for last month.
My initial idea was to cast timestamp field to time by ::time
from PostgreSQL. But Hibernate does not have this embedded function (I use HQL for building query).
It seems that there is date function but no time formatter. I do not have strict requirements for implementation and can adjust solution if it fits main idea. Now I send date boundaries as timestamps and time values as strings and do two parallel between
comparisons.
@Query("SELECT t FROM Table t WHERE t.status.id = ?1"
+ " AND t.startDate >= ?2 AND t.startDate <= ?3"
+ " AND t.startDate::time >= ?4 AND t.startDate::time <= ?5")
Page<Item> findByStatusIdAndDateRange(Long activeId, final long startDate, final long endDate, final String startTime, final String endTime, Pageable pageRequest);
This is not a production code, so no performance issues with casting values in query.
Thank you for any advice!