【问题标题】:Groovy name queried查询的 Groovy 名称
【发布时间】:2015-04-12 21:07:02
【问题描述】:

我有一个这样的域:

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

谢谢。

【问题讨论】:

  • 到目前为止您尝试过什么?什么是“多团队”?你的“域”有String parts,后来你谈到part:。这个例子不完整很难理解

标签: grails select groovy


【解决方案1】:

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))
          }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-30
    • 1970-01-01
    • 1970-01-01
    • 2014-05-21
    • 1970-01-01
    • 2012-07-08
    • 1970-01-01
    相关资源
    最近更新 更多