0

When testing my application in Swagger and trying to post one entry to MySQL database, I keep getting an error saying: java.sql.SQLIntegrityConstraintViolationException: Duplicate entry '0' for key 'PRIMARY'. When checking my stack trace, I can see the following lines before my application throws an 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 into 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' for 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 it still contained JDBC statements

It seems like there is something wrong with my entries mapping. My entry Counterparty contains an 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 those entries are parts of my Invoice entry. Maybe I made a mistake while referring from one table to another?

4
  • Do you understand the error message? "Duplicate entry '0' for key 'PRIMARY'" means you can not have two records in table addresses with primary key (i.e. address_id) = 0 (because primary key is unique) Commented Sep 12, 2017 at 11:05
  • What is the code you are using to store the object ? -- Note that the Address entity does not have a @GeneratedValue. Are you setting the Id with a value before inserting the data? Commented Sep 12, 2017 at 11:07
  • @Jaime good point, I've added @GeneratedValue(strategy = GenerationType.IDENTITY) to my address's id, however, it didn't help Commented Sep 12, 2017 at 14:12
  • What is the code you are using to store the object ? Commented Sep 12, 2017 at 16:04

1 Answer 1

7

I think the problem lies in AutoGeneration... did you manually create the TABLE or did you allow Hibernate to do it for you...
If you have Created the DB Table manually, you must have forgotten to AUTO increment the ID.
1. Try adding AutoGeneration to ID on address table too...

@Id
@Column(name = "address_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
  1. If you created the table manually, try

ALTER TABLE table_name MODIFY column_name INTEGER NOT NULL AUTO_INCREMENT;

Sign up to request clarification or add additional context in comments.

2 Comments

Hibernate creates tables for me, I've added @GeneratedValue(strategy = GenerationType.IDENTITY) to my address's id and it didn't help
well I feel, the problem is that your Auto Generation is not working somehow.. hence, it is trying to assign the address to same Id i.e 0, hence you are getting the Exception : Duplicate entry '0' for key 'PRIMARY'... is your address table empty.. if so just DROP it and let hibernate CREATE the table again... if not.. u can use an alternative, where you make this query, "SELECT MAX(id) FROM Address" store it into a variable say 'max', and setId(max+1) for the address before calling save()... I had done this once.. and it had worked...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.