hrishikeshp19
hrishikeshp19

Reputation: 9036

hibernate persist fails tries to create object with existing id

I am using spring-hibernate-postgres. I have a table say 'some_entity'. It already contains some records.

I have a program that tries to create new SomeEntity object. I am populating that object with appropriate properties, and afterwards I am calling persist() method on it.

In the log, I see that hibernate is trying to get nextVal() from the table sequences. But, nextval that my postgres returns is same as id of 2nd row of some_entity table. So, my hibernate tries to create row with that id. And hence my persist() fails with hibernate constraint violation exception.

May be I am not phrasing the question correctly. I hope someone has already encountered this problem and has resolution for it.

Thanks

Upvotes: 1

Views: 680

Answers (1)

mvb13
mvb13

Reputation: 1594

I had this problem. I solved it through execution of sql, that updates sequence at application launch

ALTER SEQUENCE names_id_seq RESTART WITH currentId;

,where currentId I get from

SELECT currval('names_id_seq');

Upvotes: 1

Related Questions