Foreign key must have same number of columns as the references primary key

Hello,

I have a table ´code_list´ in the database like this:

    | id_code_list | val_num | val_string | label    |
    | :----------: |:------: |:---------: | :------: |
    | TYP_AFF      | 3       | 3          | Other    |
    | TYP_AFF      | 1       | 1          | sec_soc  |
    | COD_LAN     | 1        | 1          | French   |

I would like to create an entity ´AffiliateType´ like this:

@Entity
@DiscriminatorValue("TYP_AFF")
public class AffiliateType extends CodeListString{

    public static final AffiliateType SOCIAL_SECRETARIAT = new AffiliateType("1");
    public static final AffiliateType VARIOUS_SERVICES = new AffiliateType("2");
    public static final AffiliateType OTHERS = new AffiliateType("3");
    public static final AffiliateType SOPA = new AffiliateType("9");

    public AffiliateType() {}

    private AffiliateType(String value) {
        super(value);
    }
}

This entity inherits from CodeList entity:

@Entity
@Table(name = "CODE_LIST")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "ID_CODE_LIST", discriminatorType = DiscriminatorType.STRING)
public abstract class CodeListString implements Serializable{

    @Id
    @Column(name = "VAL_STRING")
    protected String value;

    @Embedded
    @AttributeOverrides({
            @AttributeOverride(name = "french", column = @Column(name = "LIBELLE_F")),
            @AttributeOverride(name = "dutch", column = @Column(name = "LIBELLE_N")),
            @AttributeOverride(name = "english", column = @Column(name = "LIBELLE_X")),
            @AttributeOverride(name = "german", column = @Column(name = "LIBELLE_D"))

    })
    protected Label label;

    @Embedded
    @AttributeOverrides({
            @AttributeOverride(name = "createdBy", column = @Column(name = "AUTEUR_CREATION")),
            @AttributeOverride(name = "createdAt", column = @Column(name = "DT_CREATION")),
            @AttributeOverride(name = "modifiedBy", column = @Column(name = "AUTEUR_MODIFICATION")),
            @AttributeOverride(name = "modifiedAt", column = @Column(name = "DT_MODIFICATION"))

    })
    protected PersistenceSignature signature;

    public CodeListString() {}

    protected CodeListString(String value) {
        this.value = value;
    }

And use it in this ´Affiliate´ entity :

@Entity
@Table(name = "AFF")
public class Affiliate {

    @Id
    @Column(name = "NO_AFF")
    private int id;

    @ManyToOne(targetEntity = AffiliateType.class)
    @JoinColumn(name = "TYP_AFF")
    private AffiliateType type;

But I’m getting this error:

org.hibernate.MappingException: Foreign key (FK_BROL:aff [typ_aff])) must have same number of columns as the referenced primary key (code_list [val_string,id_code_list])

i’m using Hibernate 5.0.12.Final In a spring environment (Spring boot 1.5.10.RELEASE /JDK8)

Maybe it’s a bug. Try to replicate it with this template to replicate it on 5.2, and if you can replicate it, you should open a Jira issue.

5.0 is older than 5.2 and maybe the issue got fix, so it’s worth upgrading and checking it.