如果我有三个这样的域类:
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 :(得分:0)
在体面的Grails(2.x +)中你可以尝试类似的东西:
Application.withCriteria {
createAlias("votes", votes)
projections {
groupProperty("votes.name")
}
}
答案 1 :(得分:0)
在这种情况下,我会选择一个简单的vanilla HQL而不是Criteria,就像这样或更多(以你需要的方式使用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()