-->
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: A doubt about versioning
PostPosted: Thu Jul 09, 2009 9:02 am 
Newbie

Joined: Thu Jul 09, 2009 8:28 am
Posts: 2
Hi,

I know that hibernate increments the version number of an entity in a flush call when it's dirty.

Considering the following situation:

I have a class "Client" with an atribute called "name" and it must to be unique in my database. For this example, I have in my database two clients named: "ClientA" and "ClientB". Now let's assume I don't know that there's already a client called "ClientB" in the database and I want to rename the "ClientA" to "ClientB". Then I do the following steps:

1. Load the "ClientA" from database;
2. Change its name to "ClientB";
3. Call update to persist it with the modified name.

In this moment, hibernate increments the version number of the "Client" instance and sends the update command to the database. But, as the name of the clients must be unique, the database throws an exception. Then, to fix the problem i change the client's name to "ClientC" and try to update the same instance again (now with a different name). What happens is that hibernate throws a concurrent excpetion because the version number of the "Client" is not the same version obtained when I loaded it the first time (when the client was still named "ClientA").

My question is: Is there a best practice to handle this situation? I mean, is there a way to not get the version number incremented when exceptions are thrown from database, keeping the original version of the entity and allowing me to update it again?

Thanks in advance


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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.