更新时间:2023-09-23 13:05:58
数据库类型为TIMESTAMP
而不是DATE
,这意味着您存储了准确的时间.
The DB type is TIMESTAMP
and not DATE
, meaning you store exact times.
使用new GregorianCalendar(2012,12,5).getTime()
时,您正在查询与给定日期00:00:00.000匹配且在数据库中不存在的时间戳
When using new GregorianCalendar(2012,12,5).getTime()
you are quering timestamps that match the given date at 00:00:00.000 and that doesn't exist in your DB
您应该更改数据库以存储日期而不是时间戳,或更改查询.
You should either change the DB to store dates instead of timestamps or change your query.
JPA 2具有YEAR,MONTH和DAY功能,因此您可以
JPA 2 got YEAR, MONTH and DAY functions so you can
SELECT WHERE YEAR(yourdate) = YEAR(dbdate) AND MONTH(yourdate) = MONTH(dbdate) and DAY(yourdate) = DATE(dbdate)
在Criteria API中,您可以执行以下操作:
In Criteria API you can do something like this:
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中的date字段执行相同的操作并进行比较.
Then combine them with the AND expression and do the same for the date field in db and compare them.