2013-02-20 47 views
1

我對Grails比較陌生。 我有以下Grails 1:m獲得最多的關係

class House { 
    Integer number 
    Integer maxResidents 

    static belongsTo = [town: Town] 
} 

class Town { 
    String name 

    static hasMany = [houses: House] 
} 

我想五個鄉鎮與大多數房屋。我已經看到了創建標準的可能性,但現在我無法處理它。有人可以支持嗎? 謝謝!

回答

1

當你有一個雙向關聯,你可以查詢上House做到這一點:

def result = House.withCriteria { 
    projections { 
    groupProperty("town", "town") 
    rowCount("numHouses") 
    } 
    order("numHouses", "desc") 
    maxResults(5) 
} 

這將您返回結果的列表,其中每個結果res有鎮爲res[0]和房屋數量res[1]。如果您希望每個結果是res.townres.numHouses地圖讓訪問,那麼你應該(在你的文件的頂部與適當import一起)的maxResults行之後添加

resultTransformer(AliasToEntityMapResultTransformer.INSTANCE)