外键必须与引用的主键具有相同的列数 [英] Foreign key must have same number of columns as the referenced primary key

查看:809
本文介绍了外键必须与引用的主键具有相同的列数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个主题在这里讨论了很多次,但我仍然停留在一个异常:
外键必须与引用的主键具有相同的列数。



(我正在使用spring数据和hibernate)

我的ID类:

  @Embeddable 
public class ProxyDienstRelationPK实现可序列化{

private static final long serialVersionUID = 1L;

@Column
私人字符串parentDienstId;
@Column
private String subDienstId;

$ b public ProxyDienstRelationPK(){}
$ b $ public ProxyDienstRelationPK(ProxyDienst parentDienst,ProxyDienst subDienst){
this.parentDienstId = parentDienst.getId();
this.subDienstId = subDienst.getId();
}

// Getter和Setter,HashCode和Equals

}

我的实体:

  @Entity 
公共类ProxyDienstRelation {

@EmbeddedId
私有ProxyDienstRelationPK pdId;

私人ProxyDienst子派遣;

私人ProxyDienst parentDienst;


私有布尔模块;

// Getter and Setter
}

这个构造有以下例外:
$ b


引起:org.hibernate.MappingException:外键(FK_ad3h9gu4labg6ix34bei3poxt:proxy_dienst_relation [parent_dienst_id,sub_dienst_id]))必须与引用的主键(proxy_dienst [id])
在org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:110)
处org.hibernate.mapping.ForeignKey具有相同的列数.alignColumns(ForeignKey.java:93)
at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1816)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1739 )
在org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1424)
在org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
at org。 hibernate.jpa.boot.internal.EntityManagerFactoryB uilderImpl $ 4.perform(EntityManagerFactoryBuilderImpl.java:850)
... 23 more


希望有人可以提供帮助。谢谢

解决方案

  @MapsId(subDienstId)
@ManyToOne
private ProxyDienst subDienst;

@MapsId(parentDienstId)
@ManyToOne
私人ProxyDienst parentDienst;

您的情况 parentDienstId 是PK但也扮演着FK的角色。这被称为衍生身份。在JPA 2专书中有一篇关于它的好章节。


I know this topic is discussed a lot of times here, but I am still stuck on an exception: Foreign key must have same number of columns as the referenced primary key.

(I am using spring data and hibernate)

My ID class:

    @Embeddable
public class ProxyDienstRelationPK implements Serializable{

    private static final long serialVersionUID = 1L;

    @Column
    private String parentDienstId;
    @Column
    private String subDienstId;


    public ProxyDienstRelationPK(){}

    public ProxyDienstRelationPK(ProxyDienst parentDienst, ProxyDienst subDienst){
        this.parentDienstId = parentDienst.getId();
        this.subDienstId = subDienst.getId();
    }

   //Getter and Setter , HashCode and Equals

}

My Entity:

    @Entity
public class ProxyDienstRelation {

    @EmbeddedId
    private ProxyDienstRelationPK pdId;

    private ProxyDienst subDienst;

    private ProxyDienst parentDienst;


    private boolean modul;

    //Getter and Setter   
}

I get for this construction the following exception:

Caused by: org.hibernate.MappingException: Foreign key (FK_ad3h9gu4labg6ix34bei3poxt:proxy_dienst_relation [parent_dienst_id,sub_dienst_id])) must have same number of columns as the referenced primary key (proxy_dienst [id]) at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:110) at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:93) at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1816) at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1739) at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1424) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850) ... 23 more

Hope someone can help. Thanks

解决方案

@MapsId("subDienstId")
@ManyToOne
private ProxyDienst subDienst;

@MapsId("parentDienstId")
@ManyToOne
private ProxyDienst parentDienst;

In your case parentDienstId is part of PK but also plays role as a FK. This is called "Derived Identities". There is a good chapter about it in Pro JPA 2 book.

这篇关于外键必须与引用的主键具有相同的列数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆