I have a domain like this:
class Container {
Content content
}
class Content {
}
class ContentAlpha extends Content {
String name
}
class ContentBeta extends Content {
int length
}
Then I have a criteria:
def result = Container.withCriteria {
content {
eq('name', 'Pablo')
}
}
How can I get all the containers that contain just ContentAlpha that match the value given for the 'name'?
I think the
withCriteriaclosure you are trying to use is conceptually wrong, from the OOP perspective.Generally speaking, a superclass does not contain information about its subclasses. In contrast, a subclass does contain information about its superclass.
In your example, an instance of
ContentAlphais aContentalso. But, an instance ofContentis not aContentAlpha. Therefore, you cannot querycontent.name, becausenamedoes not exists as a property ofContentclass.Having said that, let me suggest a rudimentary tip (acknowledging that I don't know the context of the problem you are trying to solve, and assuming you can't change your domains' relations):
You could add a couple of static functions in
Container.groovy:Then you could do something like this: