I need to select some entities using HQL filtering by time. But I need to compare only date component without time. For example 1.1.2011 12.00 and 1.1.2011 13.20 must be equal.
What operator can I use?
@Query("FROM Item item " +
"WHERE item.date [equality by date operator] :date " )
List<Item> findByDate(@Param("date") Date date);
If you do not mind DB-specific functions, you'd be able to do this (using Oracle as an example).
@Query("FROM Item item WHERE trunc(item.date) = trunc(:date) " )
List<Item> findByDate(@Param("date") Date date);
You can use this because the OracleDialect registers trunc
as a StandardSQLFunction
and Oracle's TRUNC
removes the time component of a date.
If you do mind the DB-specific functions (and you should) then work with 2 dates:
then query for dates >= from date and <= to date
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With