I have kind of following table structures for Employee and Department. The composite key of Employee has three columns - EMP_ID,DEPT_ID,DEPT_CODE
But the Department has only two columns which match the composite key of Employee - DEPT_ID,DEPT_CODE.
<class name="com.test.Employee" table="EMPLOYEE">
<composite-id>
<key-property column="EMP_ID" name="empId" type="java.lang.Integer"/>
<key-property column="DEPT_ID" name="deptID" type="java.lang.Integer"/>
<key-property column="DEPT_CODE" name="deptID" type="java.lang.Integer"/>
</composite-id>
<property column="EMP_NAME" name="empName" type="java.lang.String"/>
<list name="departments" table="DEPARTMENT" fetch="select">
<key>
<column name="DEPT_ID"></column>
<column name="DEPT_CODE"></column>
</key>
<index/>
<one-to-many class="com.test.Department" />
</list>
</class>
<class name="com.test.Department" table="DEPARTMENT">
<composite-id>
<key-property column="DEPT_ID" name="deptID" type="java.lang.Integer"/>
<key-property column="DEPT_CODE" name="deptID" type="java.lang.Integer"/>
</composite-id>
<property column="DEPT_NAME" name="deptName" type="java.lang.String"/>
</class>
Now I want to put a join on these two tables but it gives error saying primary key EMP_ID is not mapped. If I put the EMP_ID under "list departments key column" in Employee mapping xml, the application loads but does not have matching column in Department table.
So is there any way to deal with this. How to configure a join/one-to-many mapping in hibernate mapping file, when you have one table with a composite key and other table whose primary key is a subset of that composite key.