使用 Hibernate 的 JPQL 类型查询中的时间戳文字

Timestamp literal in JPQL typed query with Hibernate

有没有办法在 Hibernate 的 JPQL 查询中使用时间戳文字?

JPQL 查询中应放置一个常量时间戳来与之进行比较。 现在它是这样工作的

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))

我已尝试(找到解决方案 here and here)将查询更改为以下内容

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

但在运行时出现错误

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]

我的设置是
Java 8
JPA 2.1
Spring 引导 1.4.2.RELEASE
Hibernate 5.1.0.Final(也用 5.2.16.Final 转载)
甲骨文 11.2

UPD: 可能不在规范中,但是 greatest/nvl 函数通过上面的代码从 JPQL 正确传递到 SQL。我已尝试将 greatest/nvl 包装在 function 中,但文字仍未被接受。

我在我的环境中准备了一个测试,returns 结果:

Calendar calendar = Calendar.getInstance();
calendar.setTime(date1);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);

date1 = calendar.getTime();

calendar.setTime(date2);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);

date2 = calendar.getTime();

TypedQuery<TestEntity> query = entityManager
.createQuery("select t from TestEntity t where greatest(nvl(t.f1, TO_TIMESTAMP('1970-01-01 00:00:00,000000001', 'YYYY-MM-DD HH24:MI:SS,FF9')), nvl(t.f2, TO_TIMESTAMP('1970-01-01 00:00:00,000000001', 'YYYY-MM-DD HH24:MI:SS,FF9')) between :date1 and :date2")
.setParameter("date1", date1)
.setParameter("date2", date2);

希望这就是您要找的。

编辑: 修改查询以添加 TO_TIMESTAMP 函数。 @dfche 确认它正在工作