开发者

How to query data base on Date, but your DB store date as date + time.

开发者 https://www.devze.com 2023-03-17 00:27 出处:网络
Sin开发者_C百科ce I want to store date and time for each data in table X, I use @Temporal(TemporalType.TIMESTAMP)

Sin开发者_C百科ce I want to store date and time for each data in table X, I use

@Temporal(TemporalType.TIMESTAMP)
private Date dateProcess;    //java.util.Date

and this work great. But now I want to create query that return all data in the same date. So I try

SELECT c from X c where c.dateProcess = : dateProcess

then I would pass in a java.util.Date for parameter dateProcess, but the returned result List is empty. I guess it must be that time comes into place here. So if the two dates does not have the exact date and time, it would not be equal, that would explain why my returned result list is empty. So how do I return a List of data base on a Date (dont care about time) in this scenario?


Just use a date range, specify the start and end time. > startTime and < endTime


Try TemporalType.DATE

If you need to retain the time, but only query for the date, then you have options:

  • use JPQL date function - day(..), months(..), year(..)
  • use < and > for date ranges (as James suggested)
  • store the date and time in two different columns.


Does specifiying temporal type as Date help or would you lose precision?


Currently I do like this

Using HQL to query on a date while ignoring the time on Oracle

I haven't try this one yet. So not sure it will work. date_format is registered function in release.

http://www.docjar.com/html/api/org/hibernate/dialect/MySQLDialect.java.html

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号