hibernate – 子类属性上的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 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”)

谢谢

最佳答案 你按照你的建议做到了:

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

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

点赞