开发者

Grails criteria groupby object

Example grouping by name of the zones:

def result = User.createCriteria().list{

     projections {
          roles  {
               zones{
                 开发者_如何转开发   groupProperty("name")
               }
          }
     }
}

but suppose I want to get the "id" or other attributes. the fact is that i want the object on the representing the group and not the string "name".

result.each{ println it.customFuncion() }

"zones" is a hasMany attribute and then i cant group by itself. What should be done, but doesnt works:

def result = User.createCriteria().list{

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

Is that possible? Thank you guys!


use hql for complex queries:

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

Then you can access the result columns as follows:

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

Imagine that i do not know your real hasMany collection names.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜