5
votes

I'm trying to write a SQL script to migrate data from some old table to a new one. The new table primary key (id) is managed by hibernate, so as the old table's primary key. I don't know how to assign ids for the new table using plain SQL so what i'm doing is to take the ids from the old table. The migration script works as expected.

In some point, my application tries to insert data to the new table using Hibernate, and I get primary key constraint violation exception. I suspect this is because hibernate assigning keys that collides with the keys I gave when migrating old data. BTW - hibernate is configured to assign unique primary keys per-table.

What am I doing wrong? How should I resolve this?

Thanks

2
Do you have to take the ids from old table? will it break something if you have new ids? - Tomer
I don't have to use the old ids if that helps.. - Ben Bracha

2 Answers

2
votes

If you use database with auto increment for primary (for example mysql, MSSQL)key column you have to set autoincrement value as max from existing ids (after migration). If you use sequence to generate ids (for example oracle, postgree) you have to change current sequence value to max from existing ids (after migration).

1
votes

Ok - as it turned out, when using @GeneratedValue(strategy = GenerationType.TABLE) with Hibernate, we have hibernate_sequences table that keeps track of next id for each table in my DB. So all I had to do is to update this table and set up the next id correctly - as was said here before.

Thanks!