I have a problem using the subclass-per-table mapping. I'll show the mapping/tables first (which seems to me to be the correct thing to do...)
But before that, quick statement of problem: I have two subclassed tables, childOne and childTwo. The parent object contains collections of childOne and childTwo. In some cases, the ids of childOne and childTwo are the same. Though they are different objects, Hibernate will sometimes attempt to place objects of type childOne in the collection of childTwo.
See below the mappings for a concrete example.
Tables are:
Code:
create table parentTable(integer id)
create table RelatedTable(integer parentId, integer typeId, integer childId)
create table ChildOne(integer childOneId, varchar sometext)
create table ChildTwo(integer childTwoId, varchar sometext)
The idea is simple: the parent table id relates to the parent id in RelatedTable, the discriminator column is typeId, and the id we want for the contained object is childId.
The mapping of the parent class is like so:
Code:
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cnwk.cnetapi.beans.news">
<class
name="MyParent"
table="parentTable"
mutable="true"
/>
<set name="childOneList"
lazy="false"
where="typeId=32">
<key column="parentId"/>
<one-to-many class="ChildOne"/>
</set>
<set name="childTwoList"
lazy="true"
where="typeId=8">
<key column="parentId"/>
<one-to-many class="ChildTwo"/>
</set>
</class>
</hibernate-mapping>
And the mapping with the subclassed tables:
Code:
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cnwk.cnetapi.beans.news">
<class name="RelatedStuff"
table="RelatedTable"
>
<id
name="childId"
column="childId"
type="java.lang.Integer"
>
<generator class="native">
</generator>
</id>
<discriminator column="typeId"/>
<property name="parentId"
column="parentId"
type="java.lang.Integer"
insert="false"
update="false"
/>
<property name="typeId"
column="typeId"
type="java.lang.Integer"
insert="false"
update="false"
/>
<property name="childId"
column="childId"
type="java.lang.Integer"
update="false"
insert="false"
/>
<subclass name="ChildOne" discriminator-value="32">
<join table="childOne" fetch="select">
<key column="childOneId" foreign-key="childId"/>
<property name="childOneId" column="childOneId" insert="false" update="false" />
<property name="sometext" column="sometext"/>
</join>
</subclass>
<subclass name="ChildTwo" discriminator-value="8">
<join table="childTwo" fetch="select">
<key column="childTwoId" foreign-key="childId"/>
<property name="childTwoId" column="childTwoId"
insert="false" update="false"/>
<property name="sometext" column="sometext"/>
</join>
</subclass>
</class>
</hibernate-mapping>
So...the concrete example is:
a row in childOne has a childOneId of 4444
a row in childTwo has a childTwoId of 4444
In the enclosing parent object, there are two collections:
Code:
Collection<childOne> childOneList;
Collection<childTwo> childTwoList;
When the results are read from the db ("from MyParent where id=N"), objects of type childOne end up in childTwoList. You can see where this would be troublesome.
My question: is there a mapping workaround for this? Or has anyone run into this problem before?
Please note: this is legacy data, so I can't change any of it.
Thanks for your patience if you've read this far, and thanks in advance for any pointers to a solution.