使用Hibernate Projections查找两列之间的差异

时间:2013-10-10 18:24:28

标签: hibernate

我有以下查询:

List results = getSession()
                .createCriteria(AppointmentSlot.class)
                .setProjection(
                        Projections.projectionList()
                                .add(Projections.min("startTime"))
                                .add(Projections.max("endTime"))
                                .add(Projections.groupProperty("clinicInfo"))
                                .add(Projections.groupProperty("staffInfo"))
                                .add(Projections.groupProperty("status"))
                                .add(Projections.groupProperty("dayId")))
                .list();

我想选择其他属性来计算min("startTime")max("endTime")的差异。

更新

数据库中startTimeendTime

列的示例值
  1. 08:00:00,08:15:00
  2. 08:15:00,09:00:00
  3. 09:00:00,09:15:00 ...
  4. 如何使用Hibernate实现这一点?

1 个答案:

答案 0 :(得分:2)

尝试sql投影。像

这样的东西
List results = getSession()
                .createCriteria(AppointmentSlot.class, "apps")
                .setProjection(
                        Projections.projectionList()
                                .add(Projections.min("startTime").as("minStart"))
                                .add(Projections.max("endTime").as("maxTime"))
                                .add(Projections.groupProperty("clinicInfo"))
                                .add(Projections.groupProperty("staffInfo"))
                                .add(Projections.groupProperty("status"))
                                .add(Projections.groupProperty("dayId")))
                                .add(Projections.sqlProjection("(apps.minStart- apps.maxTime) as duration", new String[]{"duration"}, new org.hibernate.type.DateType[]{new org.hibernate.type.DateType()}),"duration")
                .list();