Grails GORM按继承类的字段分离查询

Grails GORM按继承类的字段分离查询,grails,gorm,grails-domain-class,Grails,Gorm,Grails Domain Class,我有Parent类和从中继承的两个Child1和Child2: 类父{} 类Child1扩展了父类{ 字符串prop1 } 类Child2扩展了父类{ 字符串prop2 } 现在我需要编写select all Parent CLASE,但要使用prop1和prop2上的条件,例如 DetachedCriteria crit=Parent.where{ (prop1=='Some value')||(prop2=='Some value') } 标准列表() 我该怎么做呢?简单的回答是,除非子

我有
Parent
类和从中继承的两个
Child1
Child2

类父{}
类Child1扩展了父类{
字符串prop1
}
类Child2扩展了父类{
字符串prop2
}
现在我需要编写select all Parent CLASE,但要使用
prop1
prop2
上的条件,例如

DetachedCriteria crit=Parent.where{
(prop1=='Some value')||(prop2=='Some value')
}
标准列表()

我该怎么做呢?

简单的回答是,除非子类是继承的,否则不能基于子类的属性查询类。在您的示例中,父类对子类的属性没有概念,因为它们不是继承的,所以无法进行查询。最好的解决方案是分别查询每个子类,然后合并结果。

使用可以向查询中添加任意条件,因此这可能会起作用

DetachedCriteria crit = Parent.where{
    sqlRestriction 'prop1 = ? OR prop2 = ?', ['value1', 'value2]
}

唯一的缺点是单元测试中不支持SqlRestriction。

嘿,这是个好主意!