Is there a way to use timestamp literals in JPQL queries with Hibernate?
There is a constant timestamp to compare to which should be placed in JPQL query. Now it works this way
TypedQuery<TestEntity> query = entityManager
.createQuery("select t from TestEntity t where greatest(nvl(t.f1, :nullDate), nvl(t.f2, :nullDate)) between :date1 and :date2")
.setParameter("date1", date1)
.setParameter("date2", date2)
.setParameter("nulllDate", LocalDateTime.of(1970, 1, 1, 0, 0, 0, 1))
I've tried (solution found here and here) to change the query to the following
select t from TestEntity t where greatest(nvl(t.f1, {ts '1970-01-01 00:00:00:000000001'}), nvl(t.f2, {ts '1970-01-01 00:00:00:000000001'})) between :startChanges and :endChanges
but got an error in runtime
org.hibernate.QueryException: unexpected char: '{' [select t from TestEntity t where greatest(nvl(t.f1, {ts '1970-01-01 00:00:00:000000001'}), nvl(t.f2, {ts '1970-01-01 00:00:00:000000001'})) between :startChanges and :endChanges]
My setup is
Java 8
JPA 2.1
Spring Boot 1.4.2.RELEASE
Hibernate 5.1.0.Final (reproduced also with 5.2.16.Final)
Oracle 11.2
UPD: maybe it's not on the spec, but greatest/nvl functions are passed from JPQL to SQL correctly with the code above. I've tried to wrap greatest/nvl in function but the literals are still not accepted.
greatest? ornvl? Answer : it doesn't - user8558216unexpected char: '{'error for literal.select t from TestEntity t where function('greatest', function('nvl', t.f1, {ts '1970-01-01 00:00:00.000000001'}), function('nvl', t.f2, {ts '1970-01-01 00:00:00.000000001'})) between :startChanges and :endChanges- dfche