JPA Hibernate 中 String 类型的主键 Id
程序员文章站
2022-04-23 23:45:26
...
generation strategy
- Numerical identifier,数值型,非String类型主键,即 long、short、int
- 不指定具体策略
- Hibernate 将默认使用GenerationType.AUTO,根据底层数据库选择下面某一种
- identity
- sequence
- table
- Hibernate 将默认使用GenerationType.AUTO,根据底层数据库选择下面某一种
- 指定具体策略
- strategy = GenerationType.IDENTITY
- strategy = GenerationType.SEQUENCE
- strategy = GenerationType.TABL
- 不指定具体策略
- UUID identifier,String 类型
DEMO
----------------------------------------------------
通常是 Number 类型做主键
@Id
@GeneratedValue //不指定具体策略
@Column(name = "id", nullable = false)
private Long id;
----------------------------------------------------
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) //指定具体策略
@Column(name = "id", nullable = false)
private Long id;
----------------------------------------------------
特例 用 String 类型做主键
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(name = "PR_KEY")
private String prKey;
----------------------------------------------------