在Hibernate中删除复合键的冗余列

时间:2016-03-21 17:04:33

标签: java sql hibernate orm annotations

Hibernate创建空的" ID"代码中的列,如本文所述。

如何调整它以不创建" ID"列(" ID"是创建列的确切名称)或者这不能更改?

    @Entity
    @Table(name = "CATEGORY_RELATIONS")
    public class CategoryRelations implements Serializable {
    private CategoryRelationsPrimaryKey id;
    @Id
    @Column(name = "CATEGORY_RELATIONS_CATEGORY_ID")
    private String categoryId;
    @Id
    @Column(name = "CATEGORY_RELATIONS_PARENT_ID")
    private String parentId;
    //getters and setters
    @Entity
    @IdClass(CategoryRelationsPrimaryKey.class)
    public class CategoryRelationsPrimaryKey implements Serializable {
        protected long categoryId;
        protected long parentId;
        //euqals, hashCode
    }
}

1 个答案:

答案 0 :(得分:0)

1)@IdClass应该站在实体,而不是复合id类;

2)如果您已按@Id标记了ID属性,则不需要单独的id属性:

@Entity
@Table(name = "CATEGORY_RELATIONS")
@IdClass(CategoryRelationsPrimaryKey.class)
public class CategoryRelations implements Serializable {

    @Id
    @Column(name = "CATEGORY_RELATIONS_CATEGORY_ID")
    private String categoryId;

    @Id
    @Column(name = "CATEGORY_RELATIONS_PARENT_ID")
    private String parentId;

    //...

}

public class CategoryRelationsPrimaryKey implements Serializable {
    protected String categoryId;
    protected String parentId;
    // ...
}

如果您需要一些名为id的属性,请将其设为transient以避免映射到数据库表列。