I implemented an inheritance mapping by using the table per class hierarchy strategy. This works fine as long as I directly retrieve objects by id. As soon as I retrieve them via other classes (connected by foreign keys inside the database), I'm getting an object of the base class of the hierarchy only. I did it like that (simplified):
Java:
Code:
public class SampleBaseClass<T> {
private Set<SampleConnectedClass> sampleSubClass = new HashSet<SampleConnectedClass>(0);
}
public class SampleSubClass extends SampleBaseClass {
}
public class SampleConnectedClass {
private SampleBaseClass<?> sampleBaseClass;
public SampleBaseClass<?> getSample() {
return this.sampleBaseClass;
}
}
Mapping:
Code:
<hibernate-mapping>
<class name="SampleBaseClass" table="sample_base" schema="public">
...
<discriminator column="subclass_identifier" type="string" />
...
<subclass name="SampleSubClass" discriminator-value="sub" />
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="SampleConnectedClass" table="sample_connected" schema="public">
...
<many-to-one name="sample" class="SampleBaseClass" fetch="select" cascade="save-update">
<column name="id_sample_base" not-null="true" />
</many-to-one>
</class>
</hibernate-mapping>
Now when calling:
Code:
SampleConnectedClass().getSample()
I'm always getting SampleBaseClass even if it should return SampleSubClass. If I retrieve the object directly from Hibernate by id, everything works fine.
Any ideas what I am doing wrong?
Thanks in advance,
Roman