Max,
we have almost the same problem in our project. We use the same type of composite primary keys in our db tables. Because we have a java domain model that we are not free to change (at the moment JDO is used and works well at least with the domain model), we are not able to introduce extra classes for the composite ids, i.e. the composite ids have to reside as properties within the domain classes.
How can one solve the problem in this case?
A typical mapping looks like this:
Code:
<hibernate-mapping schema="dbo" package="de.vodafone.rnp.core.business.persistence.hibernate">
<class name="CellGroupImpl" table="funzgr" proxy="CellGroupImpl" lazy="true">
<composite-id>
<key-property name="workspaceId"/>
<key-property name="versionId"/>
<key-property name="name"/>
</composite-id>
<property name="workspaceId" type="integer" column="arb_lfdnr" insert="false" update="false"/>
<property name="versionId" type="integer" column="ver_nr" insert="false" update="false"/>
<property name="name" type="string" column="zgr_id"/>
<set name="cells" table="vsto_funzgm" lazy="true" inverse="true">
<key>
<column name="arb_lfdnr_1"/>
<column name="ver_nr_1"/>
<column name="zgr_id"/>
</key>
<many-to-many class="CellImpl">
<column name="arb_lfdnr_2"/>
<column name="ver_nr_2"/>
<column name="nls_id"/>
<column name="sta_id"/>
<column name="sta_loc"/>
<column name="alt_nr"/>
<column name="fan_lfdnr"/>
<column name="zel_id"/>
</many-to-many>
</set>
<many-to-one name="version" class="VersionImpl">
<column name="arb_lfdnr"/>
<column name="ver_nr"/>
</many-to-one>
</class>
</hibernate-mapping>
workspaceId and versionId are "inherited" ids from the class "one step up in the hierarchy" , i.e. VersionImpl.
A colleague of yours told me to remove the property elements for workspaceId, versionId and name and put the type and column information into the key-property element.
If I do that, Hibernate will abort processing the mapping telling me that I have to set insert= false and update= false for the repeated columns arb_lfdnr and ver_nr.
But this is not possible because these attributes are not allowed for the elements "column" or "key-property". It is only possible for the "property" element which I was told to remove.
Is the composite id scenario that the initial poster described only manageable for Hibernate if I introduce extra classes for the composite ids?
Any help would be greatly appreciated 'cause I have posted quite some questions regarding this topic in the last few days and still haven't received any answer/solution to this problem.
Thanks a lot!
Cheers,
Oliver