【发布时间】:2020-12-21 00:34:52
【问题描述】:
我想使用CriteriaQuery 执行一个查询。
查询:
select * from MY_TABLE where updated_at <= add_months(TRUNC(SYSDATE) + 1, -3)
基本上是查找表中update_at日期早于3个月的所有记录。
我尝试了this blog 提出的方法。
问题是我将-3 作为参数,但我的预期返回类型是Date。
代码:
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery(MyTable.class);
Root<MyTable> root = cq.from(MyTable.class);
Expression<Date> truncExpr = cb.function("TRUNC", Date.class, cb.currentTimestamp());
Expression<Date> addMonthsExpr = cb.function("add_months", Date.class, truncExpr, months); // <----Compilation error.
Predicate datePredicate = cb.lessThanOrEqualTo(root.get("updated_at"), addMonthsExpr);
请提出建议。
【问题讨论】:
标签: java spring hibernate jpa criteria