I'm getting a mapping exception immediately after launching my Hibernate application that says a "foreign key must have same number of columns as the referenced primary key", even though I've included the same exact composite columns as part of my FK definition in my hbm.xml files.
The exact exception reads:
Code:
Initial SessionFactory creation failed.org.hibernate.MappingException: Foreign key (FK7CCFB11A0FF9A08:sgattributepairs [SgCode])) must have same number of columns as the referenced primary key (sgcode [SgCode,Mdc])
My SQL table structure is as follows:
Code:
sgcode
=====================
-SgCode (PK - composite)
-Mdc (PK - composite)
-Description
sgcodeattributepairs
======================
-SgCode (PK - composite AND FK->sgcode - composite)
-Mdc (PK - composite AND FK->sgcode - composite)
-Flag (PK - composite AND FK->attribute)
attribute
======================
-Flag(PK)
-Type
And here are my simple two Hibernate mapping XML files. Please note that although I did cheat and use the NetBeans reverse engineering wizard to verify these are correct, I've gone through them line by line and they seem to be in accordance with hbm.xml specifications laid out in the Hibernate documentation.
Sgcode.hbm.xmlCode:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jul 13, 2011 3:23:51 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="data.pojo.Sgcode" table="sgcode" catalog="ucap">
<composite-id name="id" class="data.pojo.SgcodeId">
<key-property name="sgCode" type="string">
<column name="SgCode" length="7" />
</key-property>
<key-property name="mdc" type="int">
<column name="Mdc" />
</key-property>
</composite-id>
<property name="description" type="string">
<column name="Description" length="150" />
</property>
<set name="attributes" inverse="false" table="sgattributepairs">
<key>
<column name="Mdc" not-null="true" />
<column name="SgCode" length="7" not-null="true" />
</key>
<many-to-many entity-name="data.pojo.Attribute">
<column name="Flag" length="50" not-null="true" />
</many-to-many>
</set>
</class>
</hibernate-mapping>
Attribute.hbm.xmlCode:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jul 13, 2011 3:23:51 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="data.pojo.Attribute" table="attribute" catalog="ucap">
<id name="flag" type="string">
<column name="Flag" length="50" />
<generator class="assigned" />
</id>
<property name="type" type="java.lang.Integer">
<column name="Type" />
</property>
<set name="sgcodes" inverse="true" table="sgattributepairs">
<key>
<column name="Flag" length="50" not-null="true" />
</key>
<many-to-many entity-name="data.pojo.Sgcode">
<column name="SgCode" length="7" not-null="true" />
</many-to-many>
</set>
</class>
</hibernate-mapping>
I've been pouring over various Hibernate Many-to-Many tutorials and documents, but none have helped me shine some light on this particular version of the MappingException.
Any insight on this error what-so-ever is greatly appreciated!
This is a re-post of a question I have on Stackoverflow that's been collecting dust with no response, so feel free to answer over there as well:
http://stackoverflow.com/questions/6685415/hibernatemapping-exception-foreign-key-must-have-same-number-of-columns-as-the-r