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
|