views:

79

answers:

2

Hello guys!

I will try to summarize my question.

I have a base class "Base" with three properties. Four classes inherit from it - "A", "B", "C" and "D". They add their own additional properties. I have mapped this with InheritanceType.JOINED. Now I want to search for "Base" entities, which means that I search on the common properties of all "A", "B", "C" and "D" entities. I know how to do that. Hibernate supports it out of the box.

But I want to have a feature to search not on all types, but on a part of them. For example there are checkboxes and the user has chosen to search on "A" and "D". Again the search is on the common three properties of the "Base" class. Do I have to add an additional column to the base class to differentiate the type and populate it manually? I am sure Hibernate has something out of the box, but I can't find it.

Regards, Petar

+1  A: 

You can name multiple entities in the HQL from clause:

from A,D where prop1=:value

maybe you need to name the properties in the where clause seperately (I expect not, but you'll have to try it):

from A as aa,D as dd where aa.prop1= :value or dd.prop1= :value
EJB
EJB, thanks for the answer! I will give it a try. But I was looking for something like "from Base where prop1=:value and TypeOfChilds in (A.class, D.class). Is that possible with Hibernate?
Petar Minchev
Not that I know of, sorry.
EJB
I tried your suggestions, but both of them don't work for me. Maybe I will add an additional column "type" to the Base class to differentiate. So the query will look like this: "from Base where where prop1=:value and ((type=1) or (type=4))". But I feel there is a better solution.
Petar Minchev
A: 

I tried what I had written above and it works. So add a column "type" to the "Base" entity for differentiating between the childs. The query is: "from Base where (prop1=:value1) and (prop2=:value2) and (prop3=:value3) and (type in ('1', '4'))".

Petar Minchev