-->
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.  [ 5 posts ] 
Author Message
 Post subject: Foreign key child must have same number of columns
PostPosted: Tue May 23, 2006 3:26 pm 
Newbie

Joined: Tue May 09, 2006 1:51 pm
Posts: 2
I've seen this foriegn key error reported in the forums, but I have not seen a solution.

Basically, I have a parent with three columns that make up the primary key. A child table has a foreign key to the parent that is made up of two of the three keys. I would like a list of child items in the parent class based on a where something to the effect of parent.PK_1 = child.PK_1 and parent.PK_2 = child.PK_2.

However with the below mappings, I get org.hibernate.MappingException: Foreign key (FK33B30AA281A558:child [PK_1,PK_2])) must have same number of columns as the referenced primary key (MYPARENTTABLE [PK_1,PK_2,PK_3]).

Any help would truly be appreciated

Hibernate version:3.1.3
Mapping documents:
<hibernate-mapping>
<class name="MyParentClass" table="MYPARENTTABLE">
<composite-id name="id" class="MyParentClassId">
<key-property name="pk1" type="string" column="PK_1" />
<key-property name="pk2" type="string" column="PK_2" />
<key-property name="pk3" type="short" column="PK_3" />
</key-property>
</composite-id>
<property name="prop1" type="string" column="PROP_1" />
<property name="prop2" type="string" column="PROP_2" />
<list name="childTableList" fetch="select" lazy="true" table="CHILDTABLE">
<key>
<column name="PK_1" />
<column name="PK_2" />
</key>
<index column="PK_4"/>
<one-to-many class="MyChildClass" />
</list>
</class>
<class name="MyChildClass" table="MYCHILDTABLE">
<composite-id name="id" class="AgentId">
<key-property name="pk1" type="string" column="PK_1" />
<key-property name="pk2" type="string" column="PK_2" />
<key-property name="pk4" type="string" column="PK_4" />
</composite-id>
<property name="prop3" type="string" column="PROP_3" />
<property name="prop4" type="string" column="PROP_4" />
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
Full stack trace of any exception that occurs:
org.hibernate.MappingException: Foreign key (FK33B30AA281A558:child [PK_1,PK_2])) must have same number of columns as the referenced primary key (MYPARENTTABLE [PK_1,PK_2,PK_3])
at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:90)
at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:73)
at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1145)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1052)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1168)
at xxxxxxxxxxxxxxxx.HibernateUtil.<clinit>(HibernateUtil.java:57)
at xxxxxxxxxxxxxxxx.MyParentClassTest.testFindById(MyParentClassTest.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)


Name and version of the database you are using: DB2/NT, version: 08.01.0003
The generated SQL (show_sql=true): none is generated
Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 23, 2006 6:08 pm 
Regular
Regular

Joined: Mon May 22, 2006 2:30 pm
Posts: 74
You can't have a foreign key constraint on a primary key in another table unless the types of the keys are identical. It actually doesn't make any sense, since it implies that there could be more than one row in the PK table that matches the FK in a single row in the FK table. That pretty much breaks the relational model. Either the 3 elements identify a row uniquely, or the 2 elements do. Both tables must have the same notion of what the "unique identifier" is. I'm not sure what you are looking for in terms of a "solution".


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 24, 2006 7:39 am 
Newbie

Joined: Tue May 09, 2006 1:51 pm
Posts: 2
True, but this kind of relationship frequently occurs in the data. Is there a way to support it in hibernate.


Top
 Profile  
 
 Post subject: I have the same problem!
PostPosted: Tue Sep 25, 2007 12:22 pm 
Newbie

Joined: Sun May 28, 2006 3:19 pm
Posts: 2
Is a solution to this problem?


Top
 Profile  
 
 Post subject: this is not possible
PostPosted: Tue Sep 25, 2007 3:04 pm 
Newbie

Joined: Tue Sep 18, 2007 11:13 am
Posts: 8
both the parent table (primary key ) and the referencing foreign key should have the same number of columns.


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