hibernate - java.sql.SQLIntegrityConstraintViolationException: Duplicate entry '0' for key 'PRIMARY' -
when testing application in swagger , trying post 1 entry mysql database, keep getting error saying: java.sql.sqlintegrityconstraintviolationexception: duplicate entry '0' key 'primary'.
when checking stack trace, can see following lines before application throws error message:
2017-09-12 12:55:11.394 info 642 --- [nio-8080-exec-1] o.s.web.servlet.dispatcherservlet : frameworkservlet 'dispatcherservlet': initialization completed in 28 ms hibernate: insert addresses (house_number, street_name, town_name, zip_code, address_id) values (?, ?, ?, ?, ?) 2017-09-12 12:55:16.562 warn 642 --- [nio-8080-exec-4] o.h.engine.jdbc.spi.sqlexceptionhelper : sql error: 1062, sqlstate: 23000 2017-09-12 12:55:16.562 error 642 --- [nio-8080-exec-4] o.h.engine.jdbc.spi.sqlexceptionhelper : duplicate entry '0' key 'primary' 2017-09-12 12:55:16.563 info 642 --- [nio-8080-exec-4] o.h.e.j.b.internal.abstractbatchimpl : hhh000010: on release of batch still contained jdbc statements
it seems there wrong entries mapping. entry counterparty contains entry address (one-to-one relation)
@entity @table(name = "counterparties") public class counterparty implements serializable { @id @column(name = "counterparty_id") @generatedvalue(strategy = generationtype.identity) private int id; private int nip; private string companyname; private string phonenumber; private string bankname; private string banknumber; @onetoone(cascade = {cascadetype.persist, cascadetype.merge, cascadetype.detach, cascadetype.refresh}) @joincolumn(name = "address_id", referencedcolumnname="address_id") private address address; @onetomany(mappedby = "counterparty", fetch = fetchtype.lazy, cascade = {cascadetype.persist, cascadetype.merge, cascadetype.detach, cascadetype.refresh}) @jsonignore private list<invoice> invoices; @entity @table(name = "addresses") public class address implements serializable { @id @column(name = "address_id") private int id; private string zipcode; private string townname; private string streetname; private string housenumber; @onetoone(mappedby = "address", cascade = {cascadetype.persist, cascadetype.merge, cascadetype.detach, cascadetype.refresh}) @jsonignore private counterparty counterparty;
both of entries parts of invoice entry. maybe made mistake while referring 1 table another?
i think problem lies in autogeneration... did manually create table or did allow hibernate you...
if have created db table manually, must have forgotten auto increment id.
1. try adding autogeneration id on address table too...
@id @column(name = "address_id") @generatedvalue(strategy = generationtype.identity) private int id;
- if created table manually, try
alter table table_name modify column_name integer not null auto_increment;
Comments
Post a Comment