0

My Use Case:

  1. my Hibernate configuration using auto increment generator for insert and I don't want to change it.
  2. A user deleted a object with ID:10 and I saved this deleted object to somewhere.
  3. later on, user decide to restore this deleted object back with the same ID:10.
  4. since this object with ID:10 has been deleted from the database, How can I use Hibernate to insert it back to database while the hibernate configuration using auto increment generator(remember: I need keep the same ID for this object in database)?

Thanks,

Alex

curiousman
  • 57
  • 1
  • 1
  • 7
  • Possibly duplicate of http://stackoverflow.com/questions/3194721/bypass-generatedvalue-in-hibernate-merge-data-not-in-db ? – poison Aug 14 '12 at 15:59
  • I know it's not what you asked, and you may have a great reason for needing THAT primary key value, but: Don't design your code such that you rely on an object/row in the database having a given primary key value. (Static identity tables excepted.) In the case above, if it's possible, I'd try to design the code such that the object was recreated with a new key, and the "user" gets the new key. – Marvo Aug 14 '12 at 16:07

2 Answers2

1

I doubt Hibernate will let you do it if you don't change the generator. What about inserting it and then updating it with direct SQL and invalidating any hibernate caches?

John Watts
  • 8,717
  • 1
  • 31
  • 35
0

I doubt Hibernate will not allow you to do this. However you can go and write sql queries to update Tables [If you have used Table Generator] to change the current index position that can be used but it will hell of complicated logic since you will always have to keep track on which index record is not present

Amit Deshpande
  • 19,001
  • 4
  • 46
  • 72