开发者

Grails criteria groupby object

开发者 https://www.devze.com 2023-04-10 11:53 出处:网络
Example grouping by name of the zones: def result = User.createCriteria().list{ projections { roles{ zones{

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

精彩评论

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