我有以下查询:
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")
的差异。
更新
数据库中startTime
和endTime
如何使用Hibernate实现这一点?
答案 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();