我有一个这样的域名:
ZZPartAndTeam
String parts
String team
零件可能有很多团队。
For ex: part:part1 team:10
part:part1 team:20
part:part2 team:30
如何在域中查询获得具有多个团队的所有部分?
result:part:part1 team:10
part:part1 team:20
感谢。
答案 0 :(得分:0)
Hibernate Criteria不支持HAVING子句。一种方法是使用DetachedCriteria。
import org.hibernate.criterion.DetachedCriteria as HDetachedCriteria
query: { builder, params ->
// This query counts the number of teams per part
HDetachedCriteria innerQry = HDetachedCriteria.forClass(ZZPartAndTeam.class)
innerQry.setProjection(Projections.projectionList()
.add(Projections.count('team').as('teamCount'))
)
innerQry.add(HRestrictions.eqProperty('part', 'outer.part')
// Using innerQuery, this criteria returns the parts having more than one team.
HDetachedCriteria outerQry = HDetachedCriteria.forClass(ZZPartAndTeam.class, 'outer')
outerQry.setProjection(Projections.projectionList()
.add(Projections.distinct(Projections.property('part').as('part')))
)
outerQry.add(Subqueries.gt(1, innerQry))
builder.addToCriteria(Property.forName('part').in(outerQry))
}