Google
 
Web unafbapune.blogspot.com

Friday, June 24, 2005

 

Hibernate 3.0.5 Returns Corrupted Object via N-1 Navigation involving Subtypes ?

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 {...}
The 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!

The good news is one can get around the problem using eager fetching:
<class name="Foo" table="FOO">
...
<many-to-one name="bar" class="Bar" update="false" insert="false" lazy="false">
<column name="BAR_ID" />
</many-to-one>
...
</class>
It's all nice and cool again.

Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?