0

I have my hibernate configured as follows:

Entity
@Table (name = "statuspaatelling")
public class StatusPaaTelling {

private Long statusPaaTellingId;

@Id
@Column(name = "statusPaaTellingID")
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getStatusPaaTellingId() {
    return statusPaaTellingId;
}

When I was creating new elements it worked perfectly, but suddenly now it creates values that already exist. And I cant create new elements since i get duplicate entries for my Primary key. I am using a MySQL database.

Duplicate entry '3155220' for key 'PRIMARY'
Error Code: 1062
Call: INSERT INTO statuspaatelling (statusPaaTellingID, something) VALUES(?, ?)

I think it might first happened when i imported a dump of the database and switched to using that. I've also tried switching back with no luck, but they have the exact same setup so I should not matter. Anyone has any ideas why the GeneratedValue suddenly didn't work?

2 Answers 2

1

You are probably hitting this MySQL bug (opened 7 years ago!!!). If you read the comments, someone posted a workaround using sed to fix the MySQL dump, e.g.:

sed -i -e 's/ AUTO_INCREMENT=[0-9]\+//' mydump.sql

You could also pipe the output of mysqldump through that sed command.

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

2 Comments

Yes my dump did include AUTO_INCREMENT=xxx. And I got removed it by using the sed command, however it is still creating existing values.
Do you restart the process running Hibernate after you import the data?
-1

Have you declared the column as Auto Increment?

http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html

If not declared the column as auto increment, please follow the below link:

ALTER table - adding AUTOINCREMENT in MySQL

1 Comment

Yes I have. CREATE TABLE statuspaatelling ( statusOppdatert datetime DEFAULT NULL, statusPaaTellingID int(11) NOT NULL AUTO_INCREMENT. I has worked before, just stopped working a day ago.

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.