hql在子类属性上

时间:2012-10-03 19:06:28

标签: hibernate polymorphism hql

我有一个A类和B类和C类,它们具有不同的属性。 我该怎么做: a a a a where(a.class = B and a.specific-property-of-b =“y”)或(a.class = C and a.specific-property-of-c =“z”)

是否有可能让hibernate明白,当它是某个类的实例时,它可以访问它的特定属性,或者不可能做类似的事情,我必须这样做:

a a a a where a.id in(select b.id from b b where b.specific-property-of-b =“y”)  或a.id in(从C c中选择c.id,其中c.specific-property-of-c =“z”)

谢谢

1 个答案:

答案 0 :(得分:2)

你按照建议做到了:

select a from A a 
where (a.class = B and a.specificPropertyOfB = 'y')
or (a.class = C and a.specificPropertyOfC = 'z')

唯一不能正常工作的(根据我的经验)是在两个子类中定义两个具有相同名称的持久字段。