Hibernate supports a nice feature called polymorphic query. Say we have a table BAR with a discriminator column of two values {1,2} signifying the mutually exclusive subtypes of Bar1 and Bar2. The Java classes:
public abstract class Bar {
private long barId;
public long getBarId() { return barId; }
public void setBarId(long barId) { this.barId = barId; }
....
}
public class Bar1 extends Bar {...}
public class Bar2 extends Bar {...}
he mapping files:
<class name="Bar" table="BAR">
<id name="barId" type="long" column="BAR_ID">
<generator class="native" />
</id>
<discriminator column="TYPE" type="string" not-null="true" />
<subclass name="Bar1" discriminator-value="1" />
<subclass name="Bar2" discriminator-value="2" />
...
</class>
Now the HQL:
FROM Bar
would return instances of either Bar1 or Bar2 depending on the discriminator value. This is all nice and cool.
However, say I have a FOO table with a foreign key to BAR:
public class Foo {
...
private Bar bar;
public Bar getBar() { return bar; }
public void setBar(Bar bar) { this.bar = bar; }
}
Mapping File:
<class name="Foo" table="FOO">
...
<many-to-one name="bar" class="Bar" update="false" insert="false" >
<column name="BAR_ID" />
</many-to-one>
...
</class>
I would expect the object navigation foo.getBar() would return an instance of either Bar1 or Bar2, never Bar per se which is abstract and cannot be instantiated. e.g.
Foo foo = session.load(Foo.class, 1234);
Bar bar = foo.getBar();
The sad news is that in Hibernate 3.0.5 at least, the bar object from foo.getBar() turns out to be a CGLIB enhanced class based on Bar, not Bar1 nor Bar2. In other words, the returned object from foo.getBar() is corrupted!
For now, I got around this problem by disabling the many-to-one relationship from Foo to Bar, and just retrieved the Bar objects separately using HQL.
Is this a known bug ? Or am I mistaken ?
http://hansonchar.blogspot.com/2005/06/hibernate-305-returns-corrupted-object.html