休眠标准带有投影减去第二个投影的别名

时间:2018-07-27 12:50:59

标签: java hibernate hibernate-criteria

我需要按照最大字段«timeStop»减去最小字段«timeStart»对我的分组对象进行排序。像这样的东西:

ProjectionList projList2 = Projections.projectionList();
projList2.add(Projections.alias(Projections.max("timeStop") - Projections.min("timeStop"), "tim"));//Compilation error

Criteria criteria = sess.createCriteria(Call.class);
criteria.addOrder(up ? Order.asc("tim") : Order.desc("tim"));

我该如何实现?

1 个答案:

答案 0 :(得分:0)

使用sqlProjection

Projections.projectionList()
            .add(Projections.sqlProjection("MAX(timeStop) - MIN(timeStop) AS tim",
                    new String[] { "tim" }, new Type[] {IntegerType.INSTANCE}));

休眠类型可以在org.hibernate.type包中找到