JPA 2.0 Date Comparsin JPA Request

I am trying the following request but its throwing error.

SELECT t from Table t where t.status = :status and t.ndate > (t.ndate - 12/24) AND t.ndate < (t.ndate + 12/24)

      

Mistake

ERROR: Filter invalid. Cannot compare field ndate of type java.util.Date to value of type long. Numeric comparisons must be between numeric types only

      

Do we have any other way to do this?

+3


source to share


2 answers


(t.ndate - 12/24)

returns a numeric value, not a date, and cannot be compared to java.util.Date

.



0


source


I am assuming that you want to query if t.ndate

is between two fixed dates. Then you can calculate the date you want in Java and use those calculated date values โ€‹โ€‹(in the example, :start

and :end

) in your query. This way, you can use normal comparison and avoid introducing language dependencies of your own queries. In this case, it is better to use the comparison with BETWEEN, as it is more readable:

t.ndate BETWEEN :start AND :end

      



See also JPQL SELECT between date date and especially Java: JPQL date function to add a time period to another date .

0


source







All Articles