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.
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])
}
}
}