2014-01-06 52 views
1

的领域查询我有Parent阶级,两条Child1Child2继承它:GORM分离通过继承

class Parent { } 
class Child1 extends Parent { 
    String prop1 
} 
class Child2 extends Parent { 
    String prop2 
} 

现在我需要写选择所有家长clases,但对prop1prop2标准,即像这样的东西

DetachedCriteria crit = Parent.where { 
    (prop1 == 'Some value') || (prop2 == 'Some value') 
} 
crit.list() 

我该怎么做?

回答

1

简短的回答是,除非继承,否则不能根据子类的属性查询类。在你的例子中,父类没有子类属性的概念,因为它们没有被继承,使你的查询变得不可能。最好的解决方案是逐个查询每个子类,然后合并结果。

1

使用sqlRestriction您可以添加任意条件查询,所以这可能工作

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

唯一的缺点是,SqlRestriction没有单元测试的支持。

+0

嘿,现在这是一个好主意! –