2014-07-14 55 views
2

我知道這個主題在這裏討論了很多次,但我仍然遇到一個異常: 外鍵的列數必須與引用的主鍵相同鍵。外鍵必須與引用的主鍵具有相同的列數

(我用彈簧的數據和休眠)

我的ID類:

@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 

} 

我的實體:

@Entity 
public class ProxyDienstRelation { 

    @EmbeddedId 
    private ProxyDienstRelationPK pdId; 

    private ProxyDienst subDienst; 

    private ProxyDienst parentDienst; 


    private boolean modul; 

    //Getter and Setter 
} 

我得到這個建設中的以下異常:

造成者:org.hibernate.Map pingException:在org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:110)處,外鍵(FK_ad3h9gu4labg6ix34bei3poxt:proxy_dienst_relation [parent_dienst_id,sub_dienst_id])必須與引用的主鍵(proxy_dienst [id]) 具有相同的列數 在org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:93) 在org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1816) 在org.hibernate.cfg.Configuration.originalSecondPassCompile(配置。的java:1739) 在org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1424) 在org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844) 在org.hibernate.jpa.boot。 internal.EntityManagerFactoryBuilderImpl $ 4.perform(EntityManagerFactoryBui lderImpl.java:850) ... 23更多

希望有人可以幫忙。謝謝

回答

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

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

在你的情況下,parentDienstId是PK的一部分,但也扮演着FK的角色。這被稱爲「衍生身份」。 Pro JPA 2書中有一個很好的章節。

+0

它似乎工作。我沒有開始我的申請。非常感謝。 – LStrike

相關問題