@Entity
@IdClass(MyPK.class)
public class SubEntity {
@Id private int id;
@Id private String name;
}
@Entity
public class ParentEntity {
@OneToOne(cascade = ALL)
@JoinColumn(name="fk_sub_id", foreignKey = @ForeignKey(name="fk_sub"))
private SubEntity sub;
}
Caused by: org.hibernate.AnnotationException: A Foreign key refering SubEntity from ParentEntity has the wrong number of column. should be 2
由于@JoinColumn
注释,此操作失败。但是我如何为复合PK写它呢?我必须使用此批注在hibernate
自动生成期间设置foreignkey约束名称。
答案 0 :(得分:2)
对于复合键,您应该使用@JoinColumns
@JoinColumns(value = {
@JoinColumn(name = "fk_sub_id", referencedColumnName = "id"),
@JoinColumn(name = "fk_name", referencedColumnName = "name")},
foreignKey = @ForeignKey(name = "fk_sub"))
更新
在这种情况下,Hibernate似乎没有遵循JPA 2.1规范。您还需要另外包含hibernates自己的注释,该注释已弃用但仍然可以运行。这应该添加如下:
@JoinColumns(value = {
@JoinColumn(name = "fk_sub_id", referencedColumnName = "id"),
@JoinColumn(name = "fk_name", referencedColumnName = "name")},
foreignKey = @ForeignKey(name = "fk_sub"))
@org.hibernate.annotations.ForeignKey(name = "fk_sub")