2013-04-27 27 views
0

中如何使用「分組方式,」用Grails的標準如果我有三個域類是這樣的:三個表

class Candidate { 
    static belongsto=[application:Application] 

} 


class Vote { 
    String name; 
static belongsto=[application:Application] 

} 

class Application { 


    Date datedemand; 
    Candidate candidates; 
    Vote votes 
    static hasMany = [candidates:Candidate,votes:Vote] 

} 

我想要檢索通過投票的名稱分組的所有候選人。

我開始下面的嘗試,我仍然受阻:

def criteria = Application.createCriteria() 
def results = criteria.list { 
    votes { 

    } 
    candidates{ 


    } 

} 
+0

[see here](http://stackoverflow.com/questions/5598436/grails-createcriteria-group-by) – havenchyk 2013-04-27 10:32:21

+0

謝謝。但是,這沒有用。 – 2013-04-27 11:17:29

回答

0

在體面的Grails(2.X +),你可以嘗試類似的東西:

Application.withCriteria { 
    createAlias("votes", votes) 
    projections { 
     groupProperty("votes.name") 
    } 
} 
+0

我不認爲這是有效的。首先,'votes.name'無效。 'name'是'vote'的屬性,而不是'votes'。其次,他需要「按投票人姓名分組的所有**候選人」。 – dmahapatro 2013-04-28 00:22:52

0

在這種情況下我會去一個簡單的香草HQL而不是標準,像這樣或更多(使用join您需要的方式):

String hql = "Select V.name, C.name " + 
      "from Candidate C, Vote V " + 
      "where C.application = V.application " + 
      "group by V.name, C.name" 

Query query = session.createQuery(hql) 
List results = query.list()