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

时间:2018-09-12 09:03:30

标签: oracle spring-boot spring-data-jpa unique-constraint sequence-generators

我正在尝试使本教程(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 个答案:

答案 0 :(得分:0)

  

无论何时我通过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');