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

问题描述 投票:0回答:1

我试图让这个教程(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
oracle spring-boot spring-data-jpa unique-constraint sequence-generators
1个回答
0
投票

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

insert脚本是纯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');
© www.soinside.com 2019 - 2024. All rights reserved.