-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: problems with composite foreign key references
PostPosted: Thu Aug 18, 2005 1:29 pm 
Newbie

Joined: Thu Aug 18, 2005 5:39 am
Posts: 1
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.