Results 1 to 1 of 1
  1. #1
    jiteshks is offline Member
    Join Date
    Nov 2014
    Rep Power

    Default Composite foreign key issue

    I am getting this error :- org.hibernate.MappingException: Foreign key must have same number of columns as the referenced primary key

    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.

    I have two entities :-

    1.) - Master table having composite primary key on columns (doc_id,prod_stage).
    2.) - Child table having composite primary key on columns (doc_id,prod_stage,file_name).

    I have two primary key classes for each of the above entities.

    1.) 2.)

    Here are relevant definitions of above classes :- 1.)

    Java Code:
    @Table(name = "DOC_WEM_PRIMARY")
    public class DocWemPrimary {
    private DocWemPrimaryId docWemPrimaryId ;
    @Cascade({ CascadeType.SAVE_UPDATE, CascadeType.DELETE })
    private Set<DocWemSecondary> docWemSecondary;
    /**Other columns **/
    Java Code:
    @Table(name = "DOC_WEM_SECONDARY")
    public class DocWemSecondary {
      private DocWemPrimary docId ;
      @Column(name = "FILE_NAME" )
      private String fileName ;

    Java Code:
    public class DocWemPrimaryId implements Serializable{
    @Column(name = "DOC_ID",nullable=false)
    private String docId ;
    @Column(name = "PROD_STAGE",nullable=false)
    private String prodStage ;

    Java Code:
    public  class DocWemSecondaryId implements Serializable
            @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 ;
    The error message that I get is :-

    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])

    As you can see clearly , DocWemPrimary has two columns as primary key. Then why hibernate thinks it only has docId as primary key(as in exception message) - that part has been very puzzling to me.

    Thanks for your help.
    Last edited by jiteshks; 11-26-2014 at 06:52 PM.

Similar Threads

  1. Layers or composite?
    By trishtren in forum Java 2D
    Replies: 1
    Last Post: 04-07-2011, 09:08 PM
  2. JPA Composite Primary Key
    By The_S in forum Advanced Java
    Replies: 2
    Last Post: 02-06-2009, 07:30 AM
  3. Color Composite
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-21-2008, 09:47 PM
  4. composite key Hibernate
    By Problem in forum Web Frameworks
    Replies: 0
    Last Post: 07-25-2007, 12:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts