Hi all
The first table, CMM_CODE_VALUE, has a single-column PK called CODE_VALUE_ID. This table is on the many-side of the relationship.
The other table, CMM_SYSTEM_CODE, has a composite key of 2 columns, SYSTEM_CODE and COMPANY_ID, which have been mapped using a separate class called SystemCodePK. This table is on the one-side of the relationship.
I would like to know how I should write my mapping file. My mapping files (attached below) are giving me a MappingException on startup:
Code:
Exception in thread "main" net.sf.hibernate.MappingException: Foreign key must have same number of columns as referenced primary key
at net.sf.hibernate.mapping.ForeignKey.setReferencedTable(ForeignKey.java:33)
at net.sf.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:523)
at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:623)
at com.abc.common.dr.cmm.Test.main(Test.java:19)
Also, how do I map the child relationship using <set>, given that <set> only allows one <key> element and there are now 2 key columns?
Thanks a lot for your help.
--- Child table mapping file ---
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class
name="com.abc.common.dr.cmm.CodeValueImpl"
table="CMM_CODE_VALUE"
proxy="com.abc.common.dr.cmm.CodeValueORInterface"
dynamic-update="false"
dynamic-insert="false"
>
<id
name="codeValueId"
column="CODE_VALUE_ID"
type="long"
>
<generator class="sequence">
<param name="sequence">CMM_CODE_VALUE_ID_SQ</param>
</generator>
</id>
<!-- other properties deleted for brevity -->
<many-to-one
name="parentCodeValue"
class="com.abc.common.dr.cmm.CodeValueImpl"
cascade="none"
outer-join="auto"
update="true"
insert="true"
column="PARENT_CODE_VALUE_ID"
/>
<many-to-one
name="companyIdsystemCode"
class="com.abc.common.dr.cmm.SystemCodeImpl"
cascade="none"
outer-join="auto"
update="true"
insert="true"
>
<column name="SYSTEM_CODE"/>
<column name="COMPANY_ID"/>
</many-to-one>
<set
name="codeValuesByParentCodeValue"
table="CMM_CODE_VALUE"
lazy="true"
inverse="false"
cascade="none"
sort="unsorted"
>
<key
column="PARENT_CODE_VALUE_ID"
/>
<one-to-many
class="com.abc.common.dr.cmm.CodeValueImpl"
/>
</set>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-CodeValueImpl.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
--- Parent table mapping file ---
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class
name="com.abc.common.dr.cmm.SystemCodeImpl"
table="CMM_SYSTEM_CODE"
proxy="com.abc.common.dr.cmm.SystemCodeORInterface"
dynamic-update="false"
dynamic-insert="false"
>
<composite-id
name="compositePrimaryKey"
class="com.abc.common.dr.cmm.SystemCodePK"
>
<key-property
name="systemCode"
type="string"
column="SYSTEM_CODE"
/>
<key-property
name="company"
type="double"
column="COMPANY_ID"
/>
</composite-id>
<!-- other properties deleted for brevity -->
<set
name="codeValuesByCompanyIdsystemCode"
table="CMM_CODE_VALUE"
lazy="true"
inverse="false"
cascade="none"
sort="unsorted"
>
<key
column="COMPANY_ID"
/>
<one-to-many
class="com.abc.common.dr.cmm.CodeValueImpl"
/>
</set>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-SystemCodeImpl.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>