2016-03-21 91 views
0

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 
    } 
} 

回答

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避免映射到一个数据库表列。