What's the best way to swap unique fields? For example, if I have a unique="true" constraint on a Person.name field and I want to swap names between 2 people, I'll get a unique constraint violation when I commit the transaction:
Code:
String tempName = personA.getName();
personA.setName(personB.getName());
personB.setName(tempName);
then when the transaction gets committed, personA will have a problem because at that moment, it has the same name as personB in the database.
So we tried this:
Code:
String tempName = personA.getName();
personA.setName(null);
personB.setName(tempName);
personA.setName(personB.getName());
Setting personA to null had no effect, same unique constraint violation problem...
Now I'm about to try this:
Code:
String tempName = personA.getName();
personA.setName(null);
session.flush();
personB.setName(tempName);
personA.setName(personB.getName());
Is this the right approach? Will session.flush() cause transaction issues if I need the swap to be atomic?