QuestionComponentStatus

QuestionComponentStatus

本文介绍了如何查询特定对象类型的继承对象的集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的模型:

class Question{
  Set components
  static hasMany = [components: QuestionComponent]
}

class QuestionComponent{
  static belongsTo = Question
}

class QuestionComponentStatus extends QuestionComponent{

}
class QuestionComponentOther extends QuestionComponent{

}

我只想从Set组件中获取QuestionComponentStatus:

I want to get only QuestionComponentStatus from Set components :

questionInstance.components. ?

questionInstance.components. ?

非常感谢

推荐答案

您可以直接在子类上进行查询以避免多态结果.假设您的一对多关系是双向的(即static belongsTo = [question: Question]),则可以执行以下操作:

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)

或在HQL中:

QuestionComponentStatus.findAll("FROM QuestionComponentStatus WHERE question = :question", [question: q])

这篇关于如何查询特定对象类型的继承对象的集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 17:07