2016-03-21 90 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避免映射到一個數據庫表列。