仅记录学习笔记,如有错误欢迎指正。
记录一下oracle的触发器和自增序列的写法, 以及代码层面实现之间自增。序列:
create Sequence sequence_name increment by 2
start with 1
Nomaxvalue
noCycle
nocache;
触发器:
Create trigger trigger_name before
insert on table_name
for each row
when(new.id is null)
begin
select sequence_name.nextVal
into
:new.id
from
dual;
end
;
在对应的实体中,使用的id字段上,
加上两个注解:
@GeneratedValue(strategy = GenerationType.SEWUENCE,generator = “name1”)
@SequenceGenerator(name = “name1”,sequenceName = “sequence_name”
initialValue = 100,allocationSize= 2)
上文的"sequence_name" 就是在oracle建立的序列名称。