0

I have an entity named A, which has a createdDate with this type @Temporal(TemporalType.TIMESTAMP).

I tried to retrieve all the rows having a certain createdDate. I attached a certain date to the request and I printed that date, it looks fine like "2021-02-11 12:14:02.425", which has all the values up to millisecond.

But the sql from hibernate, the value for createdDate in the where clause is set as '11-Feb-21'. Therefore, I do not find any rows because the createdDate is saved as 2021-02-11 12:14:02.425 in the db.

public Response getByCreatedDate(Request req) {
    List<AResponse> aList = aRepository.findByIdAndCreatedDate(req.getId(), req.getCreatedDate());
}

I am new for Hibernate, I tried find some useful information about it but could not. Do I have to explicitly create a new Date with those specific date and time and send it to the method 'findByIdAndCreatedDate'? If anyone has the same experience, could you give some information about it?

Anna Lee
  • 909
  • 1
  • 17
  • 37
  • Are you using `java.util.Date`? And which Hibernate version? Asking because `Date` is poorly designed and long outdated and Hibernate 5 supports [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/), which you should prefer to use instead. – Ole V.V. Apr 24 '21 at 05:51
  • Without knowing exactly I would not expect this to work just like that. A date is not the same thing as a timestamp. So for `findByIdAndCreatedDate` to work perhaps you need to code that the timestamp is to be between 2021-02-11 00:00:00.000 (inclusive) and 2021-02-12 00:00:00.000 (exclusive) (in some time zone). – Ole V.V. Apr 24 '21 at 05:56
  • 1
    Thank you so much for your advice, I looked into it for a long time, still could not find solution, I think I have to take alternative ways. – Anna Lee Apr 24 '21 at 12:32
  • Apparently related: [Using HQL to query on a date while ignoring the time on Oracle](https://stackoverflow.com/questions/980366/using-hql-to-query-on-a-date-while-ignoring-the-time-on-oracle) – Ole V.V. Apr 24 '21 at 12:47

0 Answers0