tags:

views:

153

answers:

1

Hi,

I work with Spring/Hibernate Dao's for saving my Object in the Database. Now I had to backup all my DB inside my application. Now when I try to read my backup back, my application crashed. Now I found the problem for this crashing. It's Hibernate it creates automaticly a new ID for my Object when I want to save.

For example I saved my object a with an Id 4 in my backup file.

Now i read the backup file. Clean my DB from old stuff. Save this object back to db. and now my object id is for example 5. But it has to be 4. How can I prevent hybernate from auto generate my id value?

Should i write an extra JDBCDao for importing ?

Here is my Model attribute for id

@ID  
 @Column(name="ID")   
 @GeneratedValue(strategy=GenerationType.AUTO)
 private Long id;

Thanks for helping and exuse my bad english.

+1  A: 

Four options come to my mind:

  • make the backup using a database utility (in case of mysql for example - with mysqldump) and restore it again through a database utility, without hibernate
  • since the above doesn't seem to be an option now, you can generate the SQL queries based on your backup (show us the format of your backup) and execute them as a batch against the dtabase (again without hibernate)
  • if you don't want to use the SQL option and want to do it in hibernate, iterate your objects and save them one by one. immediately after save, update the object with the correct ID (either using .persist() or using HQL).
  • you can temporarily remove the GeneratedValue annotation while importing.

That all said, I think (again, depending on your format) that it shouldn't matter that much what the IDs are, if the referential integrity is intact.

Bozho