Goal : I want to have importJobId in ImportJob as foreign key for id of allocation table, such that when we have importJobId then and then only we can have id in allocation as without Job there cannot be any allocations.
ImportJob table has composite primary key as [ORGID,IMPORTJOBTYPE] and am trying to get create foreign key relationship in hibernate using
<id name="id"
column="ID">
<generator class="native"/>
</id>
<many-to-one name="importjobid"
class="com.delta.pdo.admin.ImportJob"
cascade="save-update"/>
in Allocation.hbm.xml which is not working out and am getting error message as:
Foreign key (FKB29B5F7366007086:ALLOCATIONS [importjobid]))
must have same number of columns as the
referenced primary key (IMPORTJOBMANAGMENT [ORGID,IMPORTJOBTYPE])
Here are my ImportJob.hbm.xml file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.delta.pdo.admin.ImportJob" table="IMPORTJOB" lazy="false">
<!-- we don't cache this since the commissions code is too screwed up to work with -->
<composite-id>
<key-property name="orgId" type="long" column="ORGID"/>
<key-property name="importJobType" type="java.lang.String" column="IMPORTJOBTYPE"/>
</composite-id>
<!-- Make sure importjobid is not-null='true '-->
<property name="importjobid" type="long" column="IMPORTJOBID" />
<property name="allocations" type="boolean" column="ALLOCATIONS" />
</class>
</hibernate-mapping>
Here are the bean classes for reference:
public class AllocationBean extends WorkbenchBeanBase
{
private static final Logger log = Logger.getLogger(AllocationBean.class);
private Float allocations;
private String importJobType;
private long id;
private long orgId;
}
public class ImportJobManagment implements Serializable
{
private long importjobid;
private long orgId;
private String importJobType;
private boolean allocations = false;
}
I have removed getter/setter
for sake of simplicity.
Update : 1 The way it is setup right now, I have id column in one table that have foreign key reference to composite key of orgId and importJobType, am not sure if we can do this and have single column foreign keyed to composite key of another table but that's my use case.
Update : 2
Thanks for awesome details, this would certainly enchance my knowledge of foreign key implementations but my final goal is to one to one mapping between two tables, where table A has composite key for identifying unique row in that table and in table B, I want to have primary key which would have foreign key reference to table A such that if we have entry in table A then same jobId entry should be in Table B, now I get your point that we cannot have single column primary key in table B that would reference composite key in table A.
so basically i want to have one to one mapping between tables where Table A has composite primary key and table B has single column primary key using hibernate, which is ofcourse am getting the mentioned error and so now i am going to create composite keys in table B also and now make foreign key reference to table A, I will verify and update my question with my findings later, again thanks for detail inputs.