How to request a collection of inherited object for a specific type of object in grails?

I have 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.

Thank you so much

+3
source share
1 answer

You can simply execute the query directly in the subclass to avoid polymorphic results. If your one-to-many relationship is bidirectional (i.e. static belongsTo = [question: Question]), you can do something like:

QuestionComponentStatus.findAllByQuestion(q)

or in HQL:

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

Source: https://habr.com/ru/post/1746994/


All Articles