Hibernate inheritance search
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
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
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'))"
.
精彩评论