------------資料表
create table t_foo(
t_id int primary key,
t_value varchar2(50) not null
);
------------序列
create sequence t_foo_0
increment by 1
start with 1
nomaxvalue
nominvalue
nocache
------------觸發器
create or replace trigger t_foo_t
before insert on t_foo
for each row
begin
select t_foo_0.nextval into:new.t_id from dual;
end;
insert into t_foo(t_value) values('test');

ps:t_id為9的資料是insert陳述句執行的
---------test代碼
TFoo tfoo = new TFoo();
tfoo.setTValue("foo100");
Session session = HibernateUtils.openSession();
Transaction ts = session.getTransaction();
ts.begin();
session.save(tfoo);
ts.commit();
session.close();

剛剛注冊的號沒多少分,麻煩各位大神幫忙
uj5u.com熱心網友回復:
沒有大神嗎?千萬別沉啊uj5u.com熱心網友回復:
沒有大神嗎?uj5u.com熱心網友回復:
理論上不會,代碼里肯定已經有1次t_foo_0.nextval,搜索下程式t_foo_0.nextval。或者讀資料的時候讀到2條記錄,而最后你只插入一條,所以看上去序列+2了uj5u.com熱心網友回復:
樓主的觸發器應該再加一個條件
create or replace trigger t_foo_t
before insert on t_foo for each row
when(new.t_id is null)
begin
select t_foo_0.nextval into:new.t_id from dual;
end;
uj5u.com熱心網友回復:
單單insert不會改變序列,看代碼上面吧uj5u.com熱心網友回復:
------------資料表create table t_foo(
t_id int primary key,
t_value varchar2(50) not null
);
------------序列
create sequence t_foo_0
increment by 1
start with 1
nomaxvalue
nominvalue
nocache
------------觸發器
create or replace trigger t_foo_t
before insert on t_foo
for each row
begin
select t_foo_0.currval into:new.t_id ,from dual;
select t_foo_0.nextval from dual;
end;
insert into t_foo(t_value) values('test');
這樣應該就行了,先取出當前值。然后再呼叫一下nextval
uj5u.com熱心網友回復:
備注:因為hibernate自帶主鍵自增轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/60616.html
標籤:開發
上一篇:ORACLE實體無法啟動
下一篇:Oracle行轉列問題
