dorcsi
dorcsi

Reputation: 325

JdbcSQLException: Unique index or primary key violation

I have two tables, AccountData and Relations. They have one to many relation, to one account can belong more relations.

I try to write a test for its repository to test the saving functionality and I get:

Caused by: org.h2.jdbc.JdbcSQLException: Unique index or primary key violation: "PRIMARY KEY ON PUBLIC.ACC_DATA(ID)"; SQL statement:
insert into ACC_DATA (ACC_CURRENCY, STATUS, CREATED_AT, CODE, ACC_NBR, OWNER_ID, EP_ID, SUBTYPE_ID, TYPE_ID, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [23505-197]

I define the primary key as follows:

@Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_ACC_DATA")
    @SequenceGenerator(name = "SEQ_ACC_DATA", sequenceName = "SEQ_ACC_DATA", allocationSize = 50)
    private Long id;

Any idea?

Thank yoou in advance!

Upvotes: 0

Views: 1690

Answers (1)

Kaushik Patel
Kaushik Patel

Reputation: 239

Code seems to be correct.

Compare the value of the SEQ_ACC_DATA Sequence in the DB and the Max value for the ID column of the ACC_DATA table.

If the ID column has value greater than the sequence, you'll need to increment the value of the Sequence so that new inserts can take place through code.

Upvotes: 1

Related Questions