From 81117ed8617b9a4351b16bd5bed33a61a4a713fc Mon Sep 17 00:00:00 2001 From: Fernando Bellas Permuy Date: Wed, 1 Jul 2009 17:09:17 +0200 Subject: [PATCH] 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). --- .../navalplanner/business/common/daos/IGenericDao.java | 8 ++++---- .../business/common/daos/impl/GenericDaoHibernate.java | 8 ++++++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/common/daos/IGenericDao.java b/navalplanner-business/src/main/java/org/navalplanner/business/common/daos/IGenericDao.java index 913b69ebb..3bb29be8c 100644 --- a/navalplanner-business/src/main/java/org/navalplanner/business/common/daos/IGenericDao.java +++ b/navalplanner-business/src/main/java/org/navalplanner/business/common/daos/IGenericDao.java @@ -46,10 +46,10 @@ public interface IGenericDao { *
* If the check is not passed, * org.springframework.dao.OptimisticLockingFailureException - * is thrown. If the key of the entity is null 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 null, + * 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); diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/common/daos/impl/GenericDaoHibernate.java b/navalplanner-business/src/main/java/org/navalplanner/business/common/daos/impl/GenericDaoHibernate.java index f227032d2..8afb7fc94 100644 --- a/navalplanner-business/src/main/java/org/navalplanner/business/common/daos/impl/GenericDaoHibernate.java +++ b/navalplanner-business/src/main/java/org/navalplanner/business/common/daos/impl/GenericDaoHibernate.java @@ -85,7 +85,7 @@ public class GenericDaoHibernate implements /* Get id and version from entity. */ Serializable id; - long versionValueInMemory; + Long versionValueInMemory; try { @@ -99,6 +99,10 @@ public class GenericDaoHibernate 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 implements return; } - if (versionValueInMemory != versionValueInDB) { + if (!versionValueInMemory.equals(versionValueInDB)) { throw new StaleObjectStateException(entityClass.getName(), id); }