Fix double version increment when creating device profiles

This commit is contained in:
VIacheslavKlimov 2025-07-23 19:03:09 +03:00
parent 24ff462b31
commit 223aae58ea

View File

@ -97,6 +97,11 @@ public abstract class JpaAbstractDao<E extends BaseEntity<D>, D>
entity = update(entity);
}
if (entity instanceof HasVersion versionedEntity) {
/*
* by default, Hibernate doesn't issue an update query and thus version increment
* if the entity was not modified. to bypass this and always increment the version, we do it manually
* */
versionedEntity.setVersion(versionedEntity.getVersion() + 1);
/*
* flushing and then removing the entity from the persistence context so that it is not affected
* by next flushes (e.g. when a transaction is committed) to avoid double version increment
@ -113,7 +118,7 @@ public abstract class JpaAbstractDao<E extends BaseEntity<D>, D>
private E create(E entity) {
if (entity instanceof HasVersion versionedEntity) {
versionedEntity.setVersion(1L);
versionedEntity.setVersion(0L);
}
if (entity.getUuid() == null) {
getEntityManager().persist(entity);
@ -147,11 +152,6 @@ public abstract class JpaAbstractDao<E extends BaseEntity<D>, D>
}
versionedEntity = entityManager.merge(versionedEntity);
entity = (E) versionedEntity;
/*
* by default, Hibernate doesn't issue an update query and thus version increment
* if the entity was not modified. to bypass this and always increment the version, we do it manually
* */
versionedEntity.setVersion(versionedEntity.getVersion() + 1);
} else {
entity = entityManager.merge(entity);
}