我有一个培训域类
class Training {
String type
Date createdOn
Date modifiedOn
static belongsTo = [course: Course]
static hasMany = [attachments: Attachment]
}
我有课程领域课程
class Course {
String name
static hasMany = [trainings: Training, tracks: Track]
static belongsTo = Track
}
和跟踪域类
class Track {
String name
}
现在我有一个过滤器(gsp页面,它将id作为参数传递给控件),根据路线和轨道选择训练
现在说params.courseId = 1和3
所以我写了查询
def query = "FROM Training AS t WHERE t.course.id IN (1,3)"
trainingList = Training.findAll(query)
这是正确的我得到了理想的输出。
现在当我说我有跟踪id的时候,params.trackId = 1,2
def query = "FROM Training AS t WHERE t.course.tracks.id IN (1,2)"
trainingList = Training.findAll(query)
这是行不通的..如何在上面说过的关联中写出正确的查询。
答案 0 :(得分:4)
区别在于您正在查询一对多关联,即“多方”。
假设HQL:
def query = "FROM Training AS t WHERE exists " +
"(from t.course.tracks AS tr where tr.id IN (1,2))"
或标准:
def trainings = Training.withCriteria {
course {
tracks {
in('id', [1, 2])
}
}
}
答案 1 :(得分:0)
我没有经历过更深入,但这个查询给出的结果与您先写的查询相同[/ p>]
def trainingList = Training.findAll ("from Training as trnin "+
"join trnin.course.id as trinincr"+
"join trinincr.tracks.id"+
" where trnin.course.id in (1,3)")