From 08c2a5479d6dfba1f42117e4c547aef343e26b9e Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Wed, 3 Apr 2024 11:22:32 +0200 Subject: [PATCH] HHH-17834 Force version increment for the correct inheritance table --- .../entity/AbstractEntityPersister.java | 55 +------------------ .../entity/JoinedSubclassEntityPersister.java | 21 +++++++ 2 files changed, 23 insertions(+), 53 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java index de92b43ab2..c5bd9143b7 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java @@ -2058,45 +2058,9 @@ public abstract class AbstractEntityPersister @Override public Object forceVersionIncrement(Object id, Object currentVersion, SharedSessionContractImplementor session) { - if ( superMappingType != null ) { - return superMappingType.getEntityPersister().forceVersionIncrement( id, currentVersion, session ); - } - + assert getMappedTableDetails().getTableName().equals( getVersionedTableName() ); final Object nextVersion = calculateNextVersion( id, currentVersion, session ); - updateCoordinator.forceVersionIncrement( id, currentVersion, nextVersion, session ); - -// // todo : cache this sql... -// String versionIncrementString = generateVersionIncrementUpdateString(); -// PreparedStatement st; -// try { -// st = session -// .getJdbcCoordinator() -// .getStatementPreparer() -// .prepareStatement( versionIncrementString, false ); -// try { -// getVersionType().nullSafeSet( st, nextVersion, 1, session ); -// getIdentifierType().nullSafeSet( st, id, 2, session ); -// getVersionType().nullSafeSet( st, currentVersion, 2 + getIdentifierColumnSpan(), session ); -// int rows = session.getJdbcCoordinator().getResultSetReturn().executeUpdate( st ); -// if ( rows != 1 ) { -// throw new StaleObjectStateException( getEntityName(), id ); -// } -// } -// finally { -// session.getJdbcCoordinator().getLogicalConnection().getResourceRegistry().release( st ); -// session.getJdbcCoordinator().afterStatementExecution(); -// } -// } -// catch (SQLException sqle) { -// throw session.getJdbcServices().getSqlExceptionHelper().convert( -// sqle, -// "could not retrieve version: " + -// MessageHelper.infoString( this, id, getFactory() ), -// getVersionSelectString() -// ); -// } - return nextVersion; } @@ -2106,12 +2070,8 @@ public abstract class AbstractEntityPersister Object currentVersion, boolean batching, SharedSessionContractImplementor session) throws HibernateException { - if ( superMappingType != null ) { - return superMappingType.getEntityPersister().forceVersionIncrement( id, currentVersion, session ); - } - + assert getMappedTableDetails().getTableName().equals( getVersionedTableName() ); final Object nextVersion = calculateNextVersion( id, currentVersion, session ); - updateCoordinator.forceVersionIncrement( id, currentVersion, nextVersion, batching, session ); return nextVersion; } @@ -2148,17 +2108,6 @@ public abstract class AbstractEntityPersister return nextVersion; } - // private String generateVersionIncrementUpdateString() { -// final Update update = new Update( getFactory().getJdbcServices().getDialect() ).setTableName( getTableName( 0 ) ); -// if ( getFactory().getSessionFactoryOptions().isCommentsEnabled() ) { -// update.setComment( "forced version increment" ); -// } -// update.addColumn( getVersionColumnName() ); -// update.addPrimaryKeyColumns( rootTableKeyColumnNames ); -// update.setVersionColumnName( getVersionColumnName() ); -// return update.toStatementString(); -// } - /** * Retrieve the version number */ diff --git a/hibernate-core/src/main/java/org/hibernate/persister/entity/JoinedSubclassEntityPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/entity/JoinedSubclassEntityPersister.java index 229b8b92d3..790458f070 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/entity/JoinedSubclassEntityPersister.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/entity/JoinedSubclassEntityPersister.java @@ -22,6 +22,7 @@ import org.hibernate.boot.model.relational.SqlStringGenerationContext; import org.hibernate.cache.spi.access.EntityDataAccess; import org.hibernate.cache.spi.access.NaturalIdDataAccess; import org.hibernate.dialect.Dialect; +import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.internal.DynamicFilterAliasGenerator; import org.hibernate.internal.FilterAliasGenerator; import org.hibernate.internal.util.collections.ArrayHelper; @@ -1082,6 +1083,26 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister { ); } + @Override + public Object forceVersionIncrement(Object id, Object currentVersion, SharedSessionContractImplementor session) { + if ( getSuperMappingType() != null ) { + return getSuperMappingType().getEntityPersister().forceVersionIncrement( id, currentVersion, session ); + } + return super.forceVersionIncrement( id, currentVersion, session ); + } + + @Override + public Object forceVersionIncrement( + Object id, + Object currentVersion, + boolean batching, + SharedSessionContractImplementor session) throws HibernateException { + if ( getSuperMappingType() != null ) { + return getSuperMappingType().getEntityPersister().forceVersionIncrement( id, currentVersion, session ); + } + return super.forceVersionIncrement( id, currentVersion, batching, session ); + } + @Override protected EntityVersionMapping generateVersionMapping( Supplier templateInstanceCreator,