组合PK会导致重复的列定义

时间:2014-07-10 08:02:51

标签: java hibernate jpa

我是JPA和Hibernate的新手,我正在尝试创建一个带有附加列位置的赋值表,它是PK的一部分,它也包含一个FK引用。

@Entity
@IdClass(ComponentAssignmentEntityPK.class)
public class ComponentAssignmentEntity implements Serializable {
    @Id
    private Integer containerID;
    private Integer elementID;
    @Id
    private Integer position;
    ....
    @ManyToOne
    @PrimaryKeyJoinColumn(name="CONTAINERID", referencedColumnName = "ID")
    public ComponentEntity getContainer() {
        return container;
    }
}

我的Key类基本上看起来像这个

public class ComponentAssignmentEntityPK implements Serializable {
    private Integer containerID;
    private Integer position;
    ....
}

但是,如果我现在使用Hibernate生成init脚本,则它包含containerid的重复定义

create table ComponentAssignment (
    containerID integer not null,
    position integer not null,
    ...
    elementID integer,
    container_id integer not null, <===
    primary key (containerID, position)
);

我做错了什么?我正在使用Hibernate 4.3.5.Final。

1 个答案:

答案 0 :(得分:0)

在您的实体中,您已定义了两个名称为CONTAINERID的列,其中一列应使用不同的名称。

因此,在生成表创建脚本时,container_id被定义为使用不同的

create table ComponentAssignment (
    containerID integer not null,    //refer to PK column name with @Id
    position integer not null,
    ...
    elementID integer,
    container_id integer not null,   //refer to column for ComponentEntity with @ManyToOne
    primary key (containerID, position)

);

假设可以通过重命名ComponentEntity的列名来解决它;

@ManyToOne
@PrimaryKeyJoinColumn(name="container_id", referencedColumnName = "ID")