开发者

How to query a collection of inherited object for a particular object type in grails?

开发者 https://www.devze.com 2022-12-31 07:13 出处:网络
I have a开发者_开发百科 this model : class Question{ Set components static hasMany = [components: QuestionComponent]

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

精彩评论

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