i create a superclass
Code:
class A{}
and 2 subclass of it
Code:
class AB extends A{}
class AC extends A{}
with mapping of
Code:
<class polymorphism="implicit" table="a" name="A">
<id unsaved-value="null" name="id">
<generator class="native"/>
</id>
<discriminator column="type"/>
<subclass name="AB" discriminator-value="ab"/>
<subclass name="AC" discriminator-value="ac"/>
</class>
and i use a collection of AB and AC in other class, namely
Code:
class X
{
private Set ab;
private Set ac;
public Set getAbs(){...}
public Set getAcs(){...}
}
with mapping of
Code:
<class table="x" name="X">
<id unsaved-value="null" name="id">
<generator class="native"/>
</id>
<set inverse="true" cascade="save-update" lazy="true" name="abs">
<key column="id_of_a"/>
<one-to-many class="AB"/>
</set>
<set inverse="true" cascade="save-update" lazy="true" name="acs">
<key column="id_of_a"/>
<one-to-many class="AC"/>
</set>
</class>
Then i populate the data of ab and ac successfully.
But when i try to call x.getAbs(), it result in a set which consist of both AB and AC
and throw an exception
Code:
org.hibernate.WrongClassException:
Object with id: 36 was not of the specified subclass: AB (loaded object was of wrong class)
It seems that hibernate query for all instance of class A (not spesific for instance of AB), and only filter the query based on the id of X.
What did i do wrong and what do i have to do to get the correct result?
Any help is appreciated.
Thanks.