前提:oracle版本需要为 12c,若之前版本,可以使用trigger+sequence实现.
步骤如下
1.配置dialec
为org.hibernate.dialect.Oracle12cDialect
2.新建表结构
关键字:generated by default as identity
(默认值,推荐)
或generated always as identity
(必须)
create table t1(id number(11) generated by default as identity primary key ,name varchar2(55));
3.旧表修改(假设表结构为t3(id number(11) primary key ,name varchar2(55)
))
ALTER TABLE T3 DROP PRIMARY KEY;
alter table t3 add idd number(11) generated by default as identity ;
update t3 set idd = id ;
ALTER TABLE T3 ADD CONSTRAINT T3_pk PRIMARY KEY (idd);
alter table t3 drop column id;
alter table t3 rename column idd to id;
4.hibernate的话,在表名指定增长策略
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")