1

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

hrishikeshp19
  • 8,838
  • 26
  • 78
  • 141
  • Sounds as though your sequence is out of sync with your tables? Could this help - [http://stackoverflow.com/questions/244243/how-to-reset-postgres-primary-key-sequence-when-it-falls-out-of-sync](http://stackoverflow.com/questions/244243/how-to-reset-postgres-primary-key-sequence-when-it-falls-out-of-sync) – Will Keeling Oct 31 '13 at 08:55

1 Answers1

1

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');
mvb13
  • 1,514
  • 3
  • 18
  • 33