0

I’m using JPA 2.1 with Hibenrate 5.1.0.Final. How do I write a JPA CriteriaBuilder query in which I’m querying for objects in a date range and I want to include the endpoints of the date range in my query? I’m noticing that the following code

final java.util.Date searchDate = objectDate.toDateTimeAtStartOfDay().toDate();
    final ParameterExpression<Date> d = builder.parameter(Date.class);
    query.where(
            …
            builder.between(d, objectRoot.<Date>get(MyObject_.objectDate), objectRoot.<Date>get(MyObject_.objectEndDate)));

This query will not return any results if the criteria falls exactly on one of the endpoints.

Dave
  • 15,639
  • 133
  • 442
  • 830
  • lessThanOrEqualTo, greaterThanOrEqualTo? Other than that you have to post the SQL generated, because debugging something like this without posting SQL is a bit pointless – Neil Stockton Apr 08 '16 at 06:26
  • Come on, Neil, you don't need to see SQL to understand the question. "between" doesn't include the endpoints, this is in their documentation. I want to include the endpoints. It sounds like lessThanOrEqualTo, greaterThanOrEqualTo is the answer here. – Dave Apr 08 '16 at 13:48

1 Answers1

0

Problems concerning date fields are almost always because of the time component. Since searchDate is the time at the start of date (DD-MON-YY 12:00am), when comparing with the low end of the BETWEEN might not match because of difference in time.

Scott Sosna
  • 1,443
  • 1
  • 8
  • 8