【问题标题】:DATEDIFF function in hibernate criteria query休眠条件查询中的 DATEDIFF 函数
【发布时间】:2020-09-22 00:21:51
【问题描述】:

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

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' 作为参数传递给方法。

那么creteria表达式怎么写呢?

【问题讨论】:

    标签: jpa hibernate-criteria datediff criteria-api


    【解决方案1】:

    我想通了。

    通过使用ParameterExpression&lt;Date&gt; param = cb.parameter(Date.class);

    我们可以做到这一点。 然后:

    TypedQuery<myVO> tq = this.entityManager.createQuery(cq);
    tq.setParameter(param, currentDate);
    return tq.getResultList();
    

    【讨论】:

      猜你喜欢
      • 2011-06-08
      • 2011-04-13
      • 2017-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多