休眠标准查询中的DATEDIFF函数

问题描述 投票:0回答:1

我必须将以下查询转换为休眠条件查询:

select * from tbl1 t1,tbl2 t2 where t1.id=t2.id 
and ( To_char(t1.modified_date, 'YYYYMMDD') - To_char(get_sys_date, 'YYYYMMDD') )>10;

其中get_sys_date是sql函数。

当我在java代码中使用datediff函数时:

 Expression<String> day = cb.literal("day");
 Expression<Integer> diff = cb.function("DATEDIFF", Integer.class, day, currentDate, 
 root.get(myVO_.modified_date));

它给我一个编译错误:

method function in interface CriteriaBuilder cannot be applied to given types

''currentDate'作为参数传递给方法。

然后如何编写标准表达式?

jpa hibernate-criteria datediff criteria-api
1个回答
0
投票

我知道了。

通过使用ParameterExpression<Date> param = cb.parameter(Date.class);

我们可以实现这一目标。然后:

TypedQuery<myVO> tq = this.entityManager.createQuery(cq);
tq.setParameter(param, currentDate);
return tq.getResultList();
© www.soinside.com 2019 - 2024. All rights reserved.