Hibernate is excelent in modelling implementation inheritance, where class A extends class B.
Suppose now that for some reason (e.g., following some advice in the GoF book) we model domain objects using interface inheritance, so that our idiomatic java code would be as follows:
interface A {
//...
}
interface B extends A {
//...
}
class AImpl implements A {
//...
}
class BImpl implements B {
private A a;
//wrapper methods for interface A
//...
}
This can be modelled in hibernate, e.g., using two distinct classes, and a many-to-one unique relationship from B to A:
<class name="AImpl"
proxy="A"
table="a">
<!-- -->
</class>
<class name="BImpl"
proxy="B"
table="b">
<!-- -->
<many-to-one name="a" column="a_id" class="AImpl" unique="true"
not-null="true"/>
</class>
When doing a polymorphic query, session.find("from A") we then get all A instances in the datastore, plus all B instances in the datastore; we therefore get twice all B instances.
I therefore guess that I am wrong and that the above is not the right way to implement interface inheritance in hibernate. I would greatly appreciate if anybody knew the correct way for interface inheritance to work with polymorphic queries without getting duplicate instances?
|