Search code examples
oraclespring-bootspring-data-jpaunique-constraintsequence-generators

Sequence Generator Oracle not woking with import.sql


Im trying to get this tutorial (https://dzone.com/articles/spring-boot-jpa-hibernate-oracle) to work.

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

Is working fine when I save the player-objects only through the application. But whenever I import data through import.sql or manually the generated ID starts again with "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');

When I run the sample code with import.sql i receive the following error:

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

Solution

  • whenever I import data through import.sql or manually the generated ID starts again with "1".

    The insert script is plain SQL and does know nothing about your entities and their mapping. Therefore it will simply insert the id that you are telling it to use

    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
    

    In order to use the sequence for inserts you, well, have to use the sequence:

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