用insert语句插入数据序列自增为1,使用hibernate执行插入数据序列就变成2了

没得感情的吃饭机器 2017-06-07 06:08:18
------------数据表
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();


刚刚注册的号没多少分,麻烦各位大神帮忙
...全文
694 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
备注:因为hibernate自带主键自增
有意1314 2017-07-04
  • 打赏
  • 举报
回复
------------数据表 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
抱枕抱枕 2017-06-11
  • 打赏
  • 举报
回复
单单insert不会改变序列,看代码上面吧
junes06 2017-06-08
  • 打赏
  • 举报
回复
楼主的触发器应该再加一个条件

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; 
sxq129601 2017-06-08
  • 打赏
  • 举报
回复
理论上不会,代码里肯定已经有1次t_foo_0.nextval,搜索下程序t_foo_0.nextval。或者读数据的时候读到2条记录,而最后你只插入一条,所以看上去序列+2了
  • 打赏
  • 举报
回复
没有大神吗?
  • 打赏
  • 举报
回复
没有大神吗?千万别沉啊

17,089

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧