I have a table that contains elements of different types... for example:
my_table
id (a long)
type (and enumerated type, eithe 'foo' or 'bar')
they can be link together in a link table
my_link_table
parent_id
child_id
I have three mappings (one parent, and two subclasses):
<hibernate-mapping>
<class name="my.ParentImpl" table="my_table" discriminator-value="null" mutable="false">
<cache usage="read-only"/>
<id name="mId" column="id" type="long" access="field">
<generator class="sequence">
<param name="sequence">foo_seq</param>
</generator>
</id>
<discriminator column="type" type="string"/>
<subclass name="my.FooImpl" discriminator-value="foo">
</subclass>
<subclass name="my.BarImpl" disciminator-value="bar">
<set name="parentFoos" table="my_link_table" access="field" lazy="true">
<key column="child_id"/>
<many-to-many column="parent_id"
class="my.FooImpl"/>
</set>
</subclass>
</hibernate-mapping>
At this point, I load an instance of BarImpl and I access the "parentFoos" collection. My hope is that it only reads values from "my_link_table" if the object referenced by "parent_id" has the correct discriminator-value. Unfortunately, even if the parent_id has a discriminator-value of "bar" it will read it from the database AND create a FooImpl with that data. This results in an entry in the cache where there is a FooImpl in the cache for an id that should actually be a BarImpl. This means that the next time I get load the BarImpl, I get the following exception:
ERROR 2004-05-27 15:05:59,453 my.FooService - Unable to retrieve related styles for: my.BarImpl@2d4c7e
net.sf.hibernate.WrongClassException: Object with id: 1346 was not of the specified subclass: my.BarImpl(loaded object was of wrong class)
at net.sf.hibernate.loader.Loader.instanceAlreadyLoaded(Loader.java:456)
at net.sf.hibernate.loader.Loader.getRow(Loader.java:423)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:209)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
at net.sf.hibernate.loader.Loader.doList(Loader.java:955)
at net.sf.hibernate.loader.Loader.list(Loader.java:946)
at net.sf.hibernate.loader.SQLLoader.list(SQLLoader.java:92)
at net.sf.hibernate.impl.SessionImpl.findBySQL(SessionImpl.java:3797)
at net.sf.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:52)
at my.BarImpl.getParents(BarImpl.java:266)
The sql that hibernate uses to initially populate the collection is:
select parentf0_.parent_id as parent_1___, parentf0_.child_id as child_2___ from my_link_table parentf0_ where parentf0_.child_id=1003
Shouldn't this sql include a clause of the sort (AND type='foo') since I set the class of my collection to FooImpl? Is this a bug? is this expected behavior? Or am I doing something wrong?
thanks
|