-->
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.  [ 3 posts ] 
Author Message
 Post subject: Foreign Key Mapping on the Composite keys.
PostPosted: Tue Jan 31, 2006 9:58 am 
Newbie

Joined: Tue Nov 29, 2005 10:36 am
Posts: 7
Hi,

How can I map between two tables having the composite primary keys on both the table. The reference key as well is a combination of two columns.

Scenario:

I created the following tables using Oracle 10g

create table POINT (
ID INTEGER not null,
CD INTEGER not null,
TYPE INTEGER,
NAME VARCHAR2(256)
)

alter table POINT
add constraint "PointPK" primary key (ID, CD)

create table POINTTYPE (
ID INTEGER not null,
TYPE INTEGER not null,
DESCR VARCHAR2(256)
)

alter table POINTTYPE
add constraint POINTTYPEPK primary key (ID, TYPE)

alter table POINT
add constraint POINTFK01 foreign key (ID, TYPE)
references POINTTYPE (ID, TYPE)

I have set up the following in the hbm file for PointType

<class name="PointType,PointType" table="POINTTYPE">
<composite-id name="PointTypeMultiPKey" class="PointTypeMultiPKey,PointType">
<key-property name="Id" column="ID" type="integer" />
<key-property name="Type" column="TYPE" type="integer" />
</composite-id>
<property name="Descr" column="DESCR" type="string" />
</class>


I give the following in hbm for Point
<class name="Point,PointType" table="POINT">
<composite-id name="PointMultiPKey" class="PointMultiPKey,PointType" >
<key-property name="Id" column="ID" type="integer" />
<key-property name="Cd" column="CD" type="integer" />
</composite-id>
<many-to-one name="PointType" unique="true" class="PointType,PointType" column="TYPE"/>
<property name="PointName" column="NAME" type="string" />
</class>

NHibernate gives error for '<many-to-one name="PointType"'

ERROR - NHibernate.MappingException: Foreign key in table POINT must have same number of columns as referenced primary key in table POINTTYPE
at NHibernate.Mapping.ForeignKey.set_ReferencedTable(Table value)
at NHibernate.Cfg.Configuration.SecondPassCompileForeignKeys(Table table, ISet done)

However, Oracle DDL scripts are creating these table without giving any error.
How can I get this working thru NHibernate?



Thanks,
Suman Sajjana


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 02, 2006 9:29 am 
Newbie

Joined: Thu Mar 02, 2006 9:18 am
Posts: 3
Hi,

I have more or less the same problem.

Did you manage to solve your problem?

As far as I'm concerned, I'd like to map those 2 tables:

continent (id_continent, id_language_data, continent)
country(id_country, id_language_data, id_continent, country)

The problem is that id_language_data is both part of the composite primary key and the composite foreign key



I think it could involve problems in updating data.
What do you think?


Thanks
Guillaume


Top
 Profile  
 
 Post subject: Does this work
PostPosted: Fri Mar 03, 2006 12:38 am 
Regular
Regular

Joined: Tue Jan 03, 2006 7:21 am
Posts: 85
I saw an example in the documentation once and according to it your mapping would need to be written as:

Code:
<many-to-one name="PointType" unique="true" class="PointType,PointType">
<column name="ID"/>
<column name="TYPE"/>
</many-to-one>


Try this to see if it works


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.