Hql by subclass property

I have class A and subclasses B and C that have different properties. How can I do something like: a from A a where (a.class = B and a.specific property-of-b = "y") or (a.class = C and a.specific property-of-c = "z")

Is it possible for hibernation to understand that when it is an instance of a particular class, then it can access its specific properties or is it impossible to do something like this, and I have to do:

a from A a, where a.id in (select b.id from B b, where b.specific property-of-b = "y") or a.id in (select c.id from C c, where c. specific property-of-c = "z")

thanks

+4
source share
1 answer

You do this as you suggested:

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

The only thing that will not work correctly (in my experience) is to define two constant fields with the same name in both subclasses.

+2
source

Source: https://habr.com/ru/post/1437664/


All Articles