ItEr15S04ArquitecturaServidorItEr14S04: improvement to "checkVersion" method.
Now the "checkVersion" method also checks if the version of the entity passed as a parameter to "checkVersion" is null. If so, the check is considered to be sucsessfull (because an entity with version == null is considered a new object, that is, an object which does not exist in the database).
This commit is contained in:
parent
dcc5c10af1
commit
81117ed861
2 changed files with 10 additions and 6 deletions
|
|
@ -46,10 +46,10 @@ public interface IGenericDao <E, PK extends Serializable>{
|
|||
* <br/>
|
||||
* If the check is not passed,
|
||||
* <code>org.springframework.dao.OptimisticLockingFailureException</code>
|
||||
* is thrown. If the key of the entity is <code>null</code> or the entity
|
||||
* does not exist in database, the check is considered to be successful.
|
||||
* This lets client code to treat creation and modification of instances in
|
||||
* a unified way.
|
||||
* is thrown. If the key or the version of the entity is <code>null</code>,
|
||||
* or the entity does not exist in database, the check is considered to be
|
||||
* successful. This lets client code to treat creation and modification of
|
||||
* instances in a unified way.
|
||||
*/
|
||||
public void checkVersion(E entity);
|
||||
|
||||
|
|
|
|||
|
|
@ -85,7 +85,7 @@ public class GenericDaoHibernate<E, PK extends Serializable> implements
|
|||
|
||||
/* Get id and version from entity. */
|
||||
Serializable id;
|
||||
long versionValueInMemory;
|
||||
Long versionValueInMemory;
|
||||
|
||||
try {
|
||||
|
||||
|
|
@ -99,6 +99,10 @@ public class GenericDaoHibernate<E, PK extends Serializable> implements
|
|||
Method getVersionMethod = entityClass.getMethod("getVersion");
|
||||
versionValueInMemory = (Long) getVersionMethod.invoke(entity);
|
||||
|
||||
if (versionValueInMemory == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
|
@ -116,7 +120,7 @@ public class GenericDaoHibernate<E, PK extends Serializable> implements
|
|||
return;
|
||||
}
|
||||
|
||||
if (versionValueInMemory != versionValueInDB) {
|
||||
if (!versionValueInMemory.equals(versionValueInDB)) {
|
||||
throw new StaleObjectStateException(entityClass.getName(), id);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue