【问题标题】:Grails criteria groupby objectGrails 标准 groupby 对象
【发布时间】:2011-10-04 22:44:18
【问题描述】:

按区域名称分组的示例:

def result = User.createCriteria().list{

     projections {
          roles  {
               zones{
                    groupProperty("name")
               }
          }
     }
}

但假设我想获取“id”或其他属性。事实是我想要代表组的对象而不是字符串“名称”。

result.each{ println it.customFuncion() }

“zones”是一个 hasMany 属性,然后我不能自己分组。应该做什么,但不起作用:

def result = User.createCriteria().list{

     projections {
          roles  {
               groupProperty("zones")
          }
     }    
}

这可能吗?谢谢各位!

【问题讨论】:

    标签: sql grails criteria


    【解决方案1】:

    使用 hql 进行复杂查询:

    def result = User.executeQuery("select u.id, zone.name from User as u inner join u.roles as role inner join role.zones as zone group by u.id, zone.name")
    

    然后您可以按如下方式访问结果列:

    result.each { row -> row[0] // u.id } // or access via defined column name
    

    想象一下,我不知道你真正的 hasMany 集合名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-07
      • 2011-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-01
      • 1970-01-01
      • 2012-06-22
      相关资源
      最近更新 更多