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:
|