We are writting Code for a legacy DB Model which requires us to use composite-id's. I have read, understood and tried the composite-id examples in the hibernate documentation. I have even looked into org.hibernate.test.cid and checked out some sample code for composite IDs. I still couldn't find a way to make hibernate work with our DB-Model.
This is the Situation:
Table Shop
-------------------------------------------------------
SHOP_ID PK
Table Customer
-------------------------------------------------------
CUSTOMER_ID PK
SHOP_ID PK,FK
Table BusinessModel
-------------------------------------------------------
MODEL_ID PK
SHOP_ID PK,FK
Table Package
-------------------------------------------------------
PACKAGE_ID PK
SHOP_ID PK,FK
MODEL_ID FK
(of course there are many more tables and almost all of
them have a composite primary key which includes the
SHOP_ID and some other Table Specific ID)
###################################
The Package table has the following composite-id:
<composite-id name="id" class="CompositePackagePK">
<key-property name="packageId" column="package_id" />
<key-property name="shopId" column="shop_id" />
</composite-id>
The Business Model has the following composite-id:
<composite-id name="id" class="CompositeBusinessModelPK">
<key-property name="businessModelId" column="model_id" />
<key-property name="shopId" column="shop_id" />
</composite-id>
Now the Package references the BusinessModell like this:
<many-to-one name="model" class="com.sony.tas.beans.BusinessModel"
insert="false" update="false" not-null="true">
<column name="model_id" />
<column name="shop_id" />
</many-to-one>
It is necessary to specify insert="false" and update="false" in order for this to work. But if model_id has insert="false" and update="false", it will never be inserted into the Package table. The only way to make this work (at least I have not found another solution yet) is to add the model_id column to the composite-id of the Package. Thus the composite-id for the Package would look like this:
<composite-id name="id" class="CompositeBusinessModelPK">
<key-property name="packageId" column="package_id" />
<key-property name="businessModelId" column="model_id" />
<key-property name="shopId" column="shop_id" />
</composite-id>
After taking the model_id into the <composite-id> of the Package mapping, model_id's are registered correctly.
Now, this Solution to the Problem certainly is not correct! Is there any elegant solution to this Problem. The root of this problem is the fact that almost every table in the DB has a composite primary keys and referencing these keys get's pretty ugly as the previous example shows.
I'm hoping that I just misunderstand something and that someone here can enlighten me.
|