-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Corrupted Object via N-1 Navigation involving subtypes
PostPosted: Sat Jun 25, 2005 8:50 am 
Beginner
Beginner

Joined: Tue Jun 21, 2005 8:38 am
Posts: 37
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.