序列生成器 Oracle 不与 import.sql 一起使用

Posted

技术标签:

【中文标题】序列生成器 Oracle 不与 import.sql 一起使用【英文标题】:Sequence Generator Oracle not woking with import.sql 【发布时间】:2018-09-12 09:03:30 【问题描述】:

我正在尝试让本教程 (https://dzone.com/articles/spring-boot-jpa-hibernate-oracle) 工作。

public class Player 
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "player_Sequence")
@SequenceGenerator(name = "player_Sequence", sequenceName = "PLAYER_SEQ")
private Long id;

当我仅通过应用程序保存播放器对象时工作正常。 但是每当我通过 import.sql 或手动导入数据时,生成的 ID 都会再次以“1”开头。

import.sql

insert into Player (id, team_id, name, num, position) values(1,1,'Lionel Messi', 10, 'Forward');
insert into Player (id, team_id, name, num, position) values(2,1,'Andreas Inniesta', 8, 'Midfielder');
insert into Player (id, team_id, name, num, position) values(3,1,'Pique', 3, 'Defender');

当我使用 import.sql 运行示例代码时,我收到以下错误:

2018-09-12 10:59:21 DEBUG org.hibernate.SQL - select team0_.id as id1_1_0_, team0_.name as name2_1_0_, players1_.team_id as team_id5_0_1_, players1_.id as id1_0_1_, players1_.id as id1_0_2_, players1_.name as name2_0_2_, players1_.num as num3_0_2_, players1_.position as position4_0_2_, players1_.team_id as team_id5_0_2_ from team team0_ left outer join player players1_ on team0_.id=players1_.team_id where team0_.id=?
2018-09-12 10:59:21 TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [1] as [BIGINT] - [1]
2018-09-12 10:59:21 DEBUG org.hibernate.SQL - select player_seq.nextval from dual
2018-09-12 10:59:21 DEBUG org.hibernate.SQL - select player_seq.nextval from dual
2018-09-12 10:59:21 DEBUG org.hibernate.SQL - insert into player (name, num, position, team_id, id) values (?, ?, ?, ?, ?)
2018-09-12 10:59:21 TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Xavi Hernandeza]
2018-09-12 10:59:21 TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [2] as [INTEGER] - [60]
2018-09-12 10:59:21 TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [3] as [VARCHAR] - [Midfielder]
2018-09-12 10:59:21 TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [4] as [BIGINT] - [1]
2018-09-12 10:59:21 TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [5] as [BIGINT] - [1]
2018-09-12 10:59:21 WARN  o.h.e.jdbc.spi.SqlExceptionHelper - SQL Error: 1, SQLState: 23000
2018-09-12 10:59:21 ERROR o.h.e.jdbc.spi.SqlExceptionHelper - ORA-00001: unique constraint (my_user_name.SYS_C007250) violated

【问题讨论】:

【参考方案1】:

每当我通过 import.sql 或手动导入数据时,生成的 ID 都会再次以“1”开头。

插入脚本是纯 SQL,对您的实体及其映射一无所知。 因此,它会简单地插入您告诉它使用的 id

insert into Player (id, team_id, name, num, position) 
             values(1,1,'Lionel Messi', 10, 'Forward');
insert into Player (id, team_id, name, num, position) 
             values(2,1,'Andreas Inniesta', 8, 'Midfielder');
insert into Player (id, team_id, name, num, position) 
             values(3,1,'Pique', 3, 'Defender');
--                  ^-- here you are providing explicite IDs

为了使用插入序列,你必须使用序列:

insert into Player (id, team_id, name, num, position) 
             values(PLAYER_SEQ.nextval,1,'Lionel Messi', 10, 'Forward');

【讨论】:

以上是关于序列生成器 Oracle 不与 import.sql 一起使用的主要内容,如果未能解决你的问题,请参考以下文章

Oracle序列生成器

Oracle 序列生成器中的保留编号

Mysql 自定义序列生成器(如 oracle)

Oracle序列

求助,oracle的序列号能不能一次获取到多个

Oracle:生成一个数字序列,其值以某个数字开头