Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare only date component in HQL

Tags:

hibernate

hql

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);
like image 266
hatesms Avatar asked Nov 17 '11 11:11

hatesms


2 Answers

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.

like image 74
beny23 Avatar answered Sep 30 '22 18:09

beny23


If you do mind the DB-specific functions (and you should) then work with 2 dates:

  1. from date = date with time set to midnight
  2. to date = date with time set to 23:59:59.999

then query for dates >= from date and <= to date

like image 34
Stijn Geukens Avatar answered Sep 30 '22 16:09

Stijn Geukens