I am looking for an elegant way to map the following scenario with Hibernate.
Table 'A' has a one-to-many relationship with B (many B rows per one A row). B rows come in two types - 'Black' and 'White'. This type is designated by an integer discriminator column in B.
There will always be exactly one Black B row, and many White B rows. I'd therefore like to map Black B as a separate property on A, and map the remaining White Bs as a list.
I did a very quick test where I tried applying a table-per-class-hierarchy mapping strategy, but I can't get the one-to-one part to work yet:
Code:
<class name="test.class.A" table="A_TABLE">
<one-to-one name="blackItem" class="test.class.B" property-ref="parentA" />
<set name="whiteItems" inverse="true">
<key column="OID" />
<one-to-many entity-name="WhiteB" />
</set>
</class>
<class name="test.class.B" table="B_TABLE" discriminator-value="1">
<id name="oid" type="long" column="OID">
<generator class="sequence">bla</generator>
</id>
<discriminator column="B_TYPE_CODE" type="integer" />
<many-to-one name="parentA" class="test.class.A" cascade="save-update">
<column name="A_OID" not-null="true" />
</many-to-one>
<subclass name="test.class.B" discriminator-value="2" entity-name="WhiteB" />
</class>
When I try to persist this, Hibernate is trying to insert NULL into the A_OID column in Black B.
Am I on the right track, or is there a better way to do this? Any suggestions would be appreciated.