Reputation: 3666
I have a Training domain class
class Training {
String type
Date createdOn
Date modifiedOn
static belongsTo = [course: Course]
static hasMany = [attachments: Attachment]
}
and i have Course domain class
class Course {
String name
static hasMany = [trainings: Training, tracks: Track]
static belongsTo = Track
}
and Track domain class
class Track {
String name
}
Now i have a filter (which is gsp page wich sends id's as a params to the control) that selects the training based on course and track
now say params.courseId = 1 and 3
so i write the query
def query = "FROM Training AS t WHERE t.course.id IN (1,3)"
trainingList = Training.findAll(query)
which is correct i i get desired output.
now when i say i have track id's , params.trackId = 1,2
def query = "FROM Training AS t WHERE t.course.tracks.id IN (1,2)"
trainingList = Training.findAll(query)
which is not working.. how to write the correct query where i have above said association.
Upvotes: 2
Views: 3846
Reputation: 5655
I haven't went through much deeper but this query giving the same result like the query you written first
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)")
Upvotes: 0
Reputation: 13475
The difference is your're querying one-to-many association, the "many" side.
Supposed HQL:
def query = "FROM Training AS t WHERE exists " +
"(from t.course.tracks AS tr where tr.id IN (1,2))"
or criteria:
def trainings = Training.withCriteria {
course {
tracks {
in('id', [1, 2])
}
}
}
Upvotes: 6