title: foreign key must have same number of columns as referenced primary key
body: <p>This error seems to be very common and many people seem to have gotten this error. But unfortunately my problem seems to be different from theirs. In my case it seems hibernate cannot find primary key as composite but rather it thinks there is only one column in primary key.</p>
  
  <p>I have two entities :-</p>
  
  <p>1.)DocWemPrimary.java - Master table having composite primary key on columns (doc_id,prod_stage).
  2.)DocWemSecondary.java - Child table having composite primary key on columns (doc_id,prod_stage,file_name).</p>
  
  <p>I have two primary key classes for each of the above entities.</p>
  
  <p>1.)DocWemPrimaryId.java
  2.)DocWemSecondaryId.java</p>
  
  <p>Here are relevant definitions of above classes :-
  1.)DocWemPrimary.java</p>
  
  <pre><code>@SuppressWarnings("serial")
  @Entity
  @Table(name = "DOC_WEM_PRIMARY")
  
  public class DocWemPrimary {
  
  @EmbeddedId
  private DocWemPrimaryId docWemPrimaryId ;
  
  @OneToMany(fetch=FetchType.LAZY,mappedBy="docId")
  @Cascade({ CascadeType.SAVE_UPDATE, CascadeType.DELETE })
  private Set&lt;DocWemSecondary&gt; docWemSecondary;
   /**Other columns **/
  }
  </code></pre>
  
  <p>2.)DocWemSecondary.java </p>
  
  <pre><code> @SuppressWarnings("serial")
   @Entity
   @Table(name = "DOC_WEM_SECONDARY")
   @IdClass(DocWemSecondaryId.class)
  
  public class DocWemSecondary {
   @Id
    @ManyToOne(fetch=FetchType.LAZY)
    private DocWemPrimary docId ;
  
  @Id
    @Column(name = "FILE_NAME" )
    private String fileName ;
  
  }
  </code></pre>
  
  <p>3.)DocWemPrimaryId.java </p>
  
  <pre><code>@Embeddable
  public class DocWemPrimaryId implements Serializable{
  
  @Column(name = "DOC_ID",nullable=false)
  private String docId ;
  
  @Column(name = "PROD_STAGE",nullable=false)
  private String prodStage ;
  }
  </code></pre>
  
  <p>4.)DocWemSecondaryId.java </p>
  
  <pre><code>@Embeddable
  public  class DocWemSecondaryId implements Serializable
  {
      @ManyToOne(fetch=FetchType.LAZY)
      @JoinColumns({
          @JoinColumn(name = "DOC_ID", referencedColumnName="DOC_ID" ,nullable = false,insertable=false, updatable=false),
          @JoinColumn(name = "PROD_STAGE", referencedColumnName="PROD_STAGE",nullable = false,insertable=false, updatable=false)})
      private DocWemPrimary docId ;
  
      @Column(name = "FILE_NAME", insertable=false, updatable=false)
      private String fileName ;
  
    }
  </code></pre>
  
  <p>The error message that I get is :-</p>
  
  <pre><code>org.hibernate.MappingException: Foreign key 
  (FK_l3fmo38mh9t2j69fpesabyegp:DOC_WEM_SECONDARY  [DOC_ID,PROD_STAGE])) 
   must have same number of columns as the referenced primary key (DOC_WEM_PRIMARY [DOC_ID])
  </code></pre>
  
  <p>As you can see clearly , DocWemPrimary has two columns as primary key. Then why hibernate thinks it only has docId as primary key - that part has been very puzzling to me.</p>
  
  <p>Thanks for your help.</p>