-->
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.  [ 12 posts ] 
Author Message
 Post subject: child table column refers only a part of parent composite-id
PostPosted: Mon Mar 08, 2004 2:45 am 
Newbie

Joined: Fri Mar 05, 2004 2:52 am
Posts: 15
hi,
pls. consider this scenario,

parent
======
id int
id1 int
name varchar

suppose composite key here is (id,id1,name)

and the related mapping is

<composite-id name="parentPK" class="ParentPK" >
<key-property name="id" column="id"/>
<key-property name="id1" column="id1"/>
<key-property name="name" column="name"/>
</composite-id>


child
====
child_id int (refers id of parent)

id1 int
child_name varchar

suppose composite key here is (child_id,id1,name)

If we map like,

<composite-id
name="childPK"
class="ChildPK" >
<key-property name="id1" column="id1"/>
<key-property name="childName" column="child_name"/>
<key-many-to-one name="parent" class="Parent" >
<column name="child_id" />

</key-many-to-one>

</composite-id >


then below exception is raised,

(cfg.Configuration 629 ) processing foreign key constraints
net.sf.hibernate.MappingException: Foreign key (child[child_id])) must have same number of columns as the referenced primary key (parent [id,id1,name])
at net.sf.hibernate.mapping.ForeignKey.setReferencedTable(ForeignKey.java:60)
at net.sf.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:649)
at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:743)



In the child table "child_id int (refers id of parent) "
and suppose we don't want to bother about other columns,
how we should provide mapping??

Thanks and Regards for any help extended!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 08, 2004 4:54 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
List all three colums of the parent composite id in the key-many-to-one - in the same order


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 09, 2004 3:11 am 
Newbie

Joined: Fri Mar 05, 2004 2:52 am
Posts: 15
Thanks michael !

But it didn't solve my problem. I will try to explain it clearly.

My doubt is can't I refer only one column of a composite key of parent from the child composite key??


Tables

Program
=========
clientID int
programID int
effectiveStartDate datetime

here composite key is (clientID,programID,effectiveStartDate)



<composite-id name="programPK" class="ProgramPK" >
<key-property name="clientID " column="clientID"/>
<key-property name="programID" column="programID"/>
<key-property name="effectiveStartDate" column="effectiveStartDate"/>
</composite-id>





Trackage
==========
trackageID int
clientID int (refers clientID of Program)
programID int (refers programID of Program)
effectiveStartDate datetime

here composite key is (trackageID,clientID,programID,effectiveStartDate)


effectiveStartDate of Trackage DOESN'T REFER effectiveStartDate of Program (BOTH ARE DIFFERENT)



<composite-id name="trackagePK" class="TrackagePK" >
<key-property name="trackageID" column="trackageID"/>
<key-property name="effectiveStartDate" column="effectiveStartDate"/>

<key-many-to-one name="program" class="Program" >
<column name="clientID" />
<column name="programID" />

<!-- <column name="effectiveStartDate" /> I DON'T WANT TO PROVIDE TAG LIKE THIS BECAUSE effectiveStartDate IS NOT REFERRED -->

</key-many-to-one>

</composite-id >


by this mapping the exception is,

cfg.Configuration 629 ) processing foreign key constraints
net.sf.hibernate.MappingException: Foreign key (Trackage[clientID,programID])) must have same number of columns as the referenced primary key (Program [clientID,programID,effectiveStartDate])
at net.sf.hibernate.mapping.ForeignKey.setReferencedTable(ForeignKey.java:60)
at net.sf.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:649)
at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:743)


Our busines requirement is like this. If not through this mapping, are there any workarounds with which we can have this sort of implementation.

IS HIBERNATE DESIGNED THIS WAY ONLY,OR AM I MISSING A POINT ?

Many thanks for your time and effort!

Thanks & Regards


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 09, 2004 11:01 am 
Beginner
Beginner

Joined: Fri Mar 05, 2004 11:14 am
Posts: 38
Location: Houston, Texas
Hi krovrav ...

I can only support you mentally by saying that I currently fight with the exact same problem. But what I have seen from Hibernate so far impressed me and I have the feeling of ther must be a way around this - I am still investigating for a solution (lots of docs to read ...)

I.e. having a Parent and child, bot with a unique surrogate key and a name field as keys but I only want to use the name for mappoing.

One option here is simply to take out the unique surrogate key of the composite-key - but than of course it is nowhere mapped that the surrogate jey actually is the primary key - hmm - well - I have felt quite stupid this week so I will check much more examples/forum posts before giving up on this one ...

B


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 09, 2004 12:55 pm 
Newbie

Joined: Tue Aug 26, 2003 2:49 pm
Posts: 11
I have the same issue.
Need help please.

Mohamed Boucetta,


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 09, 2004 1:13 pm 
Newbie

Joined: Wed Dec 10, 2003 11:49 am
Posts: 17
How about trying this. I have almost similar issue and i got it solved by doing this.

<class
name="com.xerox.hc.model.common.device.ProxyDeviceListLink"
table="PROXY_DEVICE_LIST"
>

<composite-id name="comp_id" class="com.xerox.hc.model.common.device.ProxyDeviceListLinkPK">
<key-property
name="proxyId"
column="PROXY_ID"
type="java.lang.String"
length="64"
/>
<key-property
name="serialNumber"
column="SERIAL_NUMBER"
type="java.lang.String"
length="15"
/>
</composite-id>

<!-- associations -->
<many-to-one name="deviceInfoLink" class="com.xerox.hc.model.common.device.DeviceInfoLink" column="SERIAL_NUMBER"
insert="false"
update="false"
/>
</class>

Where DeviceInfoLink is my master table (equal to program in your example). Let me know if it works.

Elango


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 11, 2004 4:10 pm 
Beginner
Beginner

Joined: Fri Mar 05, 2004 11:14 am
Posts: 38
Location: Houston, Texas
Awfull quite around this one. I still havn't found a solution. There is a related problem

http://forum.hibernate.org/viewtopic.ph ... highlight=

So I ask myselfe : Is it quite because we are just plain dum or because there is no solution for this situation ?

Thanx again to all of you out there

B


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 11, 2004 4:45 pm 
Beginner
Beginner

Joined: Fri Mar 05, 2004 11:14 am
Posts: 38
Location: Houston, Texas
I think I found the answer as the post from Gavin in a similar situation

http://forum.hibernate.org/viewtopic.ph ... criminator

there it says:

"...you have an uglyrelational model where the foreign key in link table references a unique key that is not the primary key, then this is not possible in Hibernate.

You should be able to write a UserType that executes some direct SQL and fetches a collection of objects."

agree ? hmm .. how sad .... more or less the whole posc (posc.org) Epicenter datamodel is like that if I not mistaken ...

B


Top
 Profile  
 
 Post subject: Add to FAQ?
PostPosted: Fri Mar 12, 2004 4:06 am 
Newbie

Joined: Tue Feb 17, 2004 2:51 pm
Posts: 10
Quote:
"...you have an uglyrelational model where the foreign key in link table references a unique key that is not the primary key, then this is not possible in Hibernate.


It would be nice if this could be added to the FAQ.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 12, 2004 10:33 am 
Newbie

Joined: Tue Feb 17, 2004 2:51 pm
Posts: 10
Please vote for this feature to be added in a later version of Hibernate:

http://opensource.atlassian.com/project ... key=HB-806


Top
 Profile  
 
 Post subject: I added another comment to be more specific
PostPosted: Fri Mar 12, 2004 12:21 pm 
Beginner
Beginner

Joined: Tue Nov 04, 2003 2:31 pm
Posts: 28
Location: Florida, US
Hi Pl vote for it and also visit the issue if you feel like adding more business cases for this issue.

I really wish that such feature is supported.

Thanks
Vamsi


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 22, 2004 2:44 pm 
Newbie

Joined: Fri Jan 30, 2004 3:01 pm
Posts: 3
I believe I'm in a similar situation. We have legacy tables originally defined with composite primary key, that we have switched to single column primary key. Old children in a Many-to-one relationship need the multi column foreign key, but new children use a single column foreign key. As of this writing I have not devised a solution..

Any strategies would be appreciated, and I'll post with one when I have one.


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

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.