【问题标题】:Order by multiple columns in hibernate criteria按休眠条件中的多列排序
【发布时间】:2020-01-18 13:56:43
【问题描述】:

我有一张像下面这样的表格

    ID      creationDate        startDate       status      
    1       10-08-2019 08:00    10-08-2019 08:00    1   
    2       10-08-2019 07:00    10-08-2019 08:00    2
    3       10-08-2019 09:00    10-08-2019 08:00    2
    4       10-08-2019 08:00    10-08-2019 10:00    1
    5       10-08-2019 11:00    10-08-2019 08:00    2

我编写了如下代码,通过考虑日期和状态来获取数据。最后,我想按 creationDate 和 startDate 对结果进行排序。

从 = 09-08-2019 08:00

至 = 11-08-2019 08:00

Disjunction disjunction = Restrictions.disjunction();
Conjunction creationDateConjunction = Restrictions.conjunction(); 
creationDateConjunction.add(Restrictions.in("status", 2));
creationDateConjunction.add(between("creationDate", from, to));

Conjunction startDateConjunction = Restrictions.conjunction();
startDateConjunction.add(Restrictions.in("status",1));
startDateConjunction.add(between("startDate", from, to));

disjunction.add(creationDateConjunction);
disjunction.add(startDateConjunction);
criteria.add(disjunction);
criteria.addOrder(Order.asc("creationDate"));
criteria.addOrder(Order.asc("startDate"));

我得到的结果(ID)为 2,3,5,1,4(先按createDate,然后按startDate)

但我想得到 2,1,3,4,5(随着时间的推移)

谁能帮我写一个查询以获得正确的顺序

【问题讨论】:

  • 你能展示这段代码生成的SQL语句吗?
  • 您的代码不起作用,因为您想根据另一列选择排序键。我会尝试使用 case 语句选择一个新的计算值,给它一个别名并按此列排序。

标签: sql-server hibernate


【解决方案1】:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-20
  • 2011-07-04
  • 2018-05-28
  • 2013-04-07
  • 1970-01-01
  • 2012-06-27
  • 1970-01-01
相关资源
最近更新 更多