Greetings,
I have a question on polymorphic criteria in Hibernate. Say, we have class hierarchy of three classes:
Code:
abstract class ModelObject {}
class Concrete1 extends ModelObject{}
class Concrete2 extends ModelObject{}
And we map those guys like this:
Code:
<hibernate-mapping
>
<class
name="model.ModelObject"
table="model_object"
proxy="model.ModelObject"
polymorphism="implicit"
mutable="true"
>
<discriminator
column="object_type"
type="string"
/>
<subclass
name="model.Concrete1"
discriminator-value="c1"
>
<join table="concrete1">
<key
column="v_id"
/>
/>
</join>
</subclass>
<subclass
name="model.Concrete2"
discriminator-value="c2"
>
<join table="concrete2">
<key
column="v_id"
/>
/>
</join>
</subclass>
</hibernate-mapping>
Querying the ModelObjects this way:
Code:
final ModelObject mo = (ModelObject) getSession().get(ModelObject.class, 1);
if(mo instanceof Concrete1) {
....
}
works just fine. On the other hand,
Code:
final ModelObject mo = (ModelObject) getSession().load(ModelObject.class, 1);
returns a dynamic proxy object, which cannot be cast to Concrete1 or Concrete2!
Can someone explain why this happens? Thanks in advance!