How to query a collection of inherited object for a particular object type in grails?
I have a开发者_开发百科 this model :
class Question{
Set components
static hasMany = [components: QuestionComponent]
}
class QuestionComponent{
static belongsTo = Question
}
class QuestionComponentStatus extends QuestionComponent{
}
class QuestionComponentOther extends QuestionComponent{
}
I want to get only QuestionComponentStatus from Set components :
questionInstance.components. ?
Thanks a lot
You can just do a query directly on the subclass to avoid polymorphic results. Provided that your one-to-many relationship is bi-directional (i.e. static belongsTo = [question: Question]
), you could do something like:
QuestionComponentStatus.findAllByQuestion(q)
or in HQL:
QuestionComponentStatus.findAll("FROM QuestionComponentStatus WHERE question = :question", [question: q])
精彩评论