title: Hibernate Issue : Foreign key must have same number of columns as referenced primary key
body: <p>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. </p>
  
  <p>ImportJob table has composite primary key as [ORGID,IMPORTJOBTYPE] and am trying to get create foreign key relationship in hibernate using </p>
  
  <pre><code>  &lt;id name="id"
          column="ID"&gt;
          &lt;generator class="native"/&gt;
      &lt;/id&gt;
      &lt;many-to-one name="importjobid"
                   class="com.delta.pdo.admin.ImportJob"
                   cascade="save-update"/&gt;
  </code></pre>
  
  <p>in Allocation.hbm.xml which is not working out and am getting error message as:</p>
  
  <pre><code>Foreign key (FKB29B5F7366007086:ALLOCATIONS [importjobid])) 
  must have same number of columns as the 
  referenced primary key (IMPORTJOBMANAGMENT [ORGID,IMPORTJOBTYPE])
  </code></pre>
  
  <p>Here are my ImportJob.hbm.xml file</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE hibernate-mapping PUBLIC
          "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"&gt;
  &lt;hibernate-mapping&gt;
      &lt;class name="com.delta.pdo.admin.ImportJob" table="IMPORTJOB" lazy="false"&gt;
          &lt;!-- we don't cache this since the commissions code is too screwed up to work with --&gt;
          &lt;composite-id&gt;
              &lt;key-property name="orgId" type="long" column="ORGID"/&gt;
              &lt;key-property name="importJobType" type="java.lang.String" column="IMPORTJOBTYPE"/&gt;
          &lt;/composite-id&gt;
  
          &lt;!-- Make sure importjobid is not-null='true '--&gt;
          &lt;property name="importjobid" type="long" column="IMPORTJOBID" /&gt;
          &lt;property name="allocations" type="boolean" column="ALLOCATIONS" /&gt;
      &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>Here are the bean classes for reference:</p>
  
  <pre><code>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;
  }
  </code></pre>
  
  <p>I have removed <code>getter/setter</code> for sake of simplicity.</p>
  
  <p><strong>Update : 1</strong>
  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.</p>
  
  <p><strong>Update : 2</strong> </p>
  
  <p>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.</p>
  
  <p>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.</p>