Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hql on subclass property

I have a class A and sub-classes B and C which 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 to get hibernate understand that when it's an instance of a certain class then it can access the specific properties of it or is it impossible to do something like that 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")

Thank you

like image 542
user1718057 Avatar asked Oct 03 '12 19:10

user1718057


1 Answers

You do it 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 won't work correctly (in my experience) is if you define two persistent fields with the same name in both subclasses.

like image 95
JB Nizet Avatar answered Sep 30 '22 17:09

JB Nizet