数据库类型是TIMESTAMP
并不是DATE
,这意味着您存储准确的时间。
使用时new GregorianCalendar(2012,12,5).getTime()
您正在查询与给定日期 00:00:00.000 匹配且数据库中不存在的时间戳
您应该更改数据库以存储日期而不是时间戳,或者更改您的查询。
JPA 2 具有年、月和日功能,因此您可以
SELECT WHERE YEAR(yourdate) = YEAR(dbdate) AND MONTH(yourdate) = MONTH(dbdate) and DAY(yourdate) = DATE(dbdate)
在 Criteria API 中,您可以执行以下操作:
Expression<Integer> yourdateYear = cb.function("year", Integer.class, yourdate);
Expression<Integer> yourdateMonth = cb.function("month", Integer.class, yourdate);
Expression<Integer> yourdateDay = cb.function("day", Integer.class, yourdate);
然后将它们与 AND 表达式结合起来,并对 db 中的日期字段执行相同的操作并进行比较。