Hibernate 主键策略

使用JPA通用策略生成器 

public enum GenerationType
{    
    TABLE,    
    SEQUENCE,    
    IDENTITY,    
    AUTO   
} 

 

1) IDENTITY:主键由数据库自动生成(主要是自动增长型)

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", insertable = true, nullable = false, updatable=true)
private int id;

 

2) SEQUENCE:根据底层数据库的序列来生成主键,条件是数据库支持序列。

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(name = "id", insertable = true, nullable = false, updatable=true)
private int id;

 

3) AUTO:主键由程序控制。

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", insertable = true, nullable = false, updatable=true)
private int id;

在实际使用的时候,如果使用的不是自增主键,上方的配置不起作用时,参考下面
@Id
@Column(name = "id", insertable = true, nullable = false, updatable=true)
private int id;

区别:
@GeneratedValue(strategy = GenerationType.AUTO)删除这一行
 

 

4) TABLE:使用一个特定的数据库表格来保存主键,实际工作中暂时未用到该方法

@Id
@GeneratedValue(strategy = GenerationType.TABLE)
// 未验证使用方法

 

posted @ 2015-09-25 19:27  一瞳孔  阅读(135)  评论(0)    收藏  举报