Hi all,
in our project we use Hibernate 2.1 to map our fine grained, strong typed object model to a relational database (Oracle).
We have a variety of code/description classes mapped onto a single database table, each of them being a subclass of a CodeModel class distinguished by a discriminator. This works fine for reading the individual code/description classes, but to our disappointment we cannot (to our knowledge) refer to an individual code/description object from a referring object using Hibernate mapping.
As a temporary solution we read related CODE objects by hand-coded statements, but this is not what we like...
Is there a solution to this problem in Hibernate?
Below a simplified example showing how we would like to specify the mapping.
With this mapping, Hibernate has sufficient information to resolve the references.
The current problem is that in the ClientModel, there's only one attribute, whereas there are 2 fields in the PK of the MaritalStateModel. However, one field of the PK if fixed (Discriminator)
The example shows the mapping of a ClientModel class referring to a GenderModel and a MaritalStateModel, both subclasses of CodeModel.
Code:
<hibernate-mapping>
<class name="app.model.CodeModel" table="CODE">
<composite-id name="codepk" class="app.model.CodePKModel">
<key-property name="prefix"/>
<key-property name="code"/>
</composite-id>
<discriminator column="PREFIX" type="string"/>
<property name="order"/>
<property name="description"/>
<subclass name="app.model.GenderModel" discriminator-value="GENDER"/>
<subclass name="app.model.MaritalStateModel" discriminator-value="MARSTATE"/>
</class>
<class name="app.model.ClientModel" table="CLIENT">
<composite-id name="clientnr" class="app.model.ClientnrModel">
<key-property name="clientnr"/>
</composite-id>
<property name="name"/>
<property name="town"/>
<many-to-one name="gender"
column="GENDERCODE" class="app.model.GenderModel"/>
<many-to-one name="maritalstate"
column="MARITALSTATECODE" class="app.model.MaritalStateModel"/>
</class>
</hibernate-mapping>