开发者

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])
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜