-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Trouble swapping unique fields among 2 rows
PostPosted: Tue Nov 25, 2008 2:48 pm 
Regular
Regular

Joined: Mon Mar 10, 2008 6:40 pm
Posts: 114
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?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.