Manually Update The Counter Used For Generation Of Primary Key In Hibernate?
Solution 1:
Call this SQL query once per table to set the sequence to the next free number:
SELECT setval('tblname_id_seq', max(id)) FROM tblname;
tblname being the actual name of the table.
Hibernate may use a different naming convention, or the sequence may have been renamed. If you can't find the sequence behind the serial column, check with (per documentation):
SELECT pg_get_serial_sequence(tblname, column_name)
More details: Modify Django AutoField start valueHow to import a CSV to postgresql that already has ID's assigned?
Solution 2:
The problem here might be that you declare the id as a primitive instead of a wrapper.
So instead of:
privateint id;
You should have:
privateInteger id;
When you create the entity with the id is initialized as 0, instead of NULL.
That's why you get duplicate id constraint violation exceptions.
Only when the id is NULL the AUTO generation strategy will delegate the id assignment to the database.
Post a Comment for "Manually Update The Counter Used For Generation Of Primary Key In Hibernate?"