0

为什么我的休眠应用程序中出现以下异常。

Caused by: org.hibernate.MappingException: Foreign key (FK2C47CFCAB95CF1D: EMP [ADDR_ID,ADDR_ZIP])) must have same number of columns as the referenced primary key (ADDRESS [ID_COL])
    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:1714)
    at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1637)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1350)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737)
    at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:94)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:920)
    ... 9 more

对于我的映射

@ManyToOne
@JoinColumns({
    @JoinColumn(name="ADDR_ID", referencedColumnName="ID"),
    @JoinColumn(name="ADDR_ZIP", referencedColumnName="ZIP")
})
@NotFound(action=NotFoundAction.IGNORE)
private AddressEntity addressEntity;

我的 AddressEntity 如下所示

public AddressEntity {
    @Id
    @Column(name = "ID_COL")
    private String item;

    @Column(name = "ID")
    private String ID;

    @Column(name = "ZIP")
    private String ZIP;

    ...
    ...

}

JoinColumn 不需要指向主键。但仍然发生异常

4

1 回答 1

0

您的问题出在连接列定义中。您只能使用键列声明映射。

@ManyToOne
@JoinColumn(name="ADDRESS_COL", referencedColumnName="ID_COL")
@NotFound(action=NotFoundAction.IGNORE)
private AddressEntity addressEntity;

public AddressEntity {

    @Id
    @Column(name = "ID_COL")
    private String item;

    @Column(name = "ID")
    private String ID;

    @Column(name = "ZIP")
    private String ZIP;

    ...
}

whereADDRESS_COL必须是您的 emp 表中引用ID_COL您的地址表的外键列。

于 2013-09-19T12:41:41.447 回答