------------数据表
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();
刚刚注册的号没多少分,麻烦各位大神帮忙

解决方案 »

  1.   

    理论上不会,代码里肯定已经有1次t_foo_0.nextval,搜索下程序t_foo_0.nextval。或者读数据的时候读到2条记录,而最后你只插入一条,所以看上去序列+2了
      

  2.   

    楼主的触发器应该再加一个条件
    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; 
      

  3.   

    单单insert不会改变序列,看代码上面吧
      

  4.   

    ------------数据表
    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
      

  5.   

    备注:因为hibernate自带主键自增