How to find by date from timestamp column in JPA criteria?

牧云@^-^@ 提交于 2019-11-29 15:28:58

The DB type is TIMESTAMP and not DATE, meaning you store exact times.

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 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)

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);

Then combine them with the AND expression and do the same for the date field in db and compare them.

The SIMPLEST WAY to compare date of two datetimes

compare two date and ignore time

WHERE DATE(dbdDate) = DATE(yourDate)

Michel

You can use the native query in the jpql.

Example(SQL server):

select table from table where convert(date,mydate)=:date_colum
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!