通过JPA的策略生成器来进行生成:JPA hibernate 其他的ORM框架照样支持
GenerationType.TABLE,SEQUENCE
Hiberante : foriegn uuid
自定义主键生成方式
TABLE缓存主键生成器
@TableGenerator(name = “pk_gen”,
table=”tb_generator”,
pkColumnName=”gen_name”,
valueColumnName=”gen_value”,
pkColumnValue=”STUDENT_PK”,
allocationSize=1
)
CREATE TABLE tb_generator (
gen_name VARCHAR2(255) NOT NULL,
gen_value NUMBER NOT NULL,
PRIMARY KEY(gen_name)
)
SEQUENCE生成器
@SequenceGenerator(name=”seq_studnet”, sequenceName=”seq_payment”)
IDENTITY主键生成器:mysql mmsql
@GeneratedValue(strategy = GenerationType.IDENTITY)
AUTO:主键生成器:根据宿主数据库进行选择 如果是mysql 自动增长 如果是oracle sequence
@GeneratedValue(strategy = GenerationType.AUTO)
hibernate自己提供的丰富的主键生成策略:
assigned手动指派
@GenericGenerator(name = “studentgenerator”, strategy = “assigned”)
sequence
@GenericGenerator(name = “studentgenerator”, strategy = “sequence”,
parameters = { @Parameter(name = “sequence”, value = “seq_Student”) })
native:对于 oracle 采用 Sequence 方式,对于MySQL 和 SQL Server 采用identity(自增主键生成机制)
@GeneratedValue(generator = “studentgenerator”)
@GenericGenerator(name = “studentgenerator”, strategy = “native”)
identity:mysql sqlserver Oracle 不支持自增字段
uuid: 采用128位的uuid算法生成主键,uuid被编码为一个32位16进制数字的字符串。占用空间大(字符串类型)
@GeneratedValue(generator = “studentgenerator”)
@GenericGenerator(name = “studentgenerator”, strategy = “uuid”)
foreign:外键
@GeneratedValue(generator = “idGenerator”)
@GenericGenerator(name = “idGenerator”, strategy = “foreign”,
parameters = { @Parameter(name = “empid”, value = “employee”) })
很少用的算法 :了解即可
hilo:使用hilo生成策略,要在数据库中建立一张额外的表 使用高地位算法
@GeneratedValue(generator = “studentgenerator”)
@GenericGenerator(name = “studentgenerator”, strategy = “hilo”)
increment:hibernate 自增长,多线程不适合
@GeneratedValue(generator = “studentgenerator”)
@GenericGenerator(name = “studentgenerator”, strategy = “increment”)
guid:采用数据库底层的guid算法机制
@Entity
@Table(name="TB_STUDENT")
public class Student {
//@Id用来标识主属性列
@Id
private Long id;
主键id未配置默认的,在使用的时候需要手动指定主键的值。
public class FirstTest {
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
Student s = new Student();
s.setName("lily");
session.save(s);
session.close();
}
}
并未指定id值,看控制台打印消息:
从控制台中可以看出,使用默认的主键生成策略需要手动去添加主键的值。
Student s = new Student();
s.setId(1l);
s.setName("lily");
并未提交事物,所以数据库方面不会有操作,此时以及没有了语法错误。
@Entity
@Table(name="TB_STUDENT")
@TableGenerator(name = "pk_gen",
table="tb_generator",
pkColumnName="gen_name",
valueColumnName="gen_value",
pkColumnValue="STUDENT_PK",
allocationSize=1
)
public class StudentTwo {
//@Id用来标识主属性列
@Id
@GeneratedValue(strategy=GenerationType.TABLE,generator="pk_gen")
private Long id;
private String name;
控制台打印的消息:
五月 14, 2017 11:32:06 上午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete
Hibernate:
select
gen_value
from
tb_generator
where
gen_name = 'STUDENT_PK' for update
Hibernate:
insert
into
tb_generator
(gen_name, gen_value)
values
('STUDENT_PK', ?)
Hibernate:
update
tb_generator
set
gen_value = ?
where
gen_value = ?
and gen_name = 'STUDENT_PK'
Hibernate:
select
gen_value
from
tb_generator
where
gen_name = 'STUDENT_PK' for update
Hibernate:
update
tb_generator
set
gen_value = ?
where
gen_value = ?
and gen_name = 'STUDENT_PK'
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;