【问题标题】:How to compare two column value with hibernate Restrictions如何将两列值与休眠限制进行比较
【发布时间】:2018-04-24 09:15:47
【问题描述】:

我无法将 MySQL Query 转换为适当的 Hibernate 代码。具体如下SQL

select a.id, b.id as id, b.timestamp as timestamp, b.speed as speed from abc a join xyz b on b.abc_id = a.id left xyz b2 on (a.id = b2.abc_id and b.timestamp < b2.timestamp OR b.timestamp = b2.timestamp AND b.id < b2.id))

上面的查询运行完美,得到了预期的结果,但是当我尝试转换为 Hibernate Criteria 时,我不知道下面的代码

(a.id = b2.abc_id and (b.timestamp < b2.timestamp OR b.timestamp = b2.timestamp AND b.id < b2.id))

我已经尝试过以下代码

Criteria criteria = currentSession().createCriteria(Abc.class);
criteria.createAlias("xyz", "b");
criteria.createAlias("xyz", "b2");
criteria.add(Restrictions.le("b.timestamp", "b2.timestamp"));
criteria.add(Restrictions.lt("b.id", "b2.id"));

但它不能按照 MySql Query 工作

【问题讨论】:

  • criteria.add(Restrictions.le("___"); 将被解析为SQL中'where'之后的条件。所以你尝试的解决方案代码不正确。
  • 试试 Restrictions.leProperty("field1","field2")

标签: hibernate hibernate-criteria jointable


【解决方案1】:

我知道这是一个老问题,但对于寻找答案的人,您可以尝试以下代码:

Criteria cr = session.createCriteria(YourClass.class, "entity1");
cr.add(Restrictions.ltProperty("field1", "field2"));
...

【讨论】:

    猜你喜欢
    • 2014-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-18
    • 2013-05-09
    • 1970-01-01
    • 2019-12-27
    • 1970-01-01
    相关资源
    最近更新 更多