diff --git a/hibernate-core/src/main/java/org/hibernate/boot/spi/SessionFactoryOptions.java b/hibernate-core/src/main/java/org/hibernate/boot/spi/SessionFactoryOptions.java index 2939955db7..e899feeb98 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/spi/SessionFactoryOptions.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/spi/SessionFactoryOptions.java @@ -131,6 +131,11 @@ public interface SessionFactoryOptions extends QueryEngineOptions { BaselineSessionEventsListenerBuilder getBaselineSessionEventsListenerBuilder(); + /** + * Should generated identifiers be reset after entity removal? + * + * @see org.hibernate.cfg.AvailableSettings#USE_IDENTIFIER_ROLLBACK + */ boolean isIdentifierRollbackEnabled(); boolean isCheckNullability(); diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java b/hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java index 7ade5077bb..2d4d1ea55b 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java @@ -170,6 +170,7 @@ public interface AvailableSettings /** * When enabled, specifies that the generated identifier of an entity is unset * when the entity is {@linkplain org.hibernate.Session#remove(Object) deleted}. + * If the entity is versioned, the version is also reset to its default value. * * @settingDefault {@code false} - generated identifiers are not unset * diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/VersionValue.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/VersionValue.java index 9497431d04..73bcb12db2 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/VersionValue.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/VersionValue.java @@ -80,8 +80,8 @@ public class VersionValue implements UnsavedValueStrategy { if ( version == null ) { return Boolean.TRUE; } - if ( version instanceof Number ) { - return ((Number) version).longValue() < 0L; + if ( version instanceof Number number ) { + return number.longValue() < 0L; } throw new MappingException( "unsaved-value NEGATIVE may only be used with short, int and long types" ); } 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 36e28ac231..8a33cffd45 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 @@ -4359,14 +4359,11 @@ public abstract class AbstractEntityPersister Object currentId, Object currentVersion, SharedSessionContractImplementor session) { - if ( entityMetamodel.getIdentifierProperty().getGenerator().allowAssignedIdentifiers() ) { - return; + if ( !getGenerator().allowAssignedIdentifiers() ) { + // reset the identifier + final Object defaultIdentifier = identifierMapping.getUnsavedStrategy().getDefaultValue( currentId ); + setIdentifier( entity, defaultIdentifier, session ); } - - // reset the identifier - final Object defaultIdentifier = identifierMapping.getUnsavedStrategy().getDefaultValue( currentId ); - setIdentifier( entity, defaultIdentifier, session ); - // reset the version if ( versionMapping != null ) { final Object defaultVersion = versionMapping.getUnsavedStrategy().getDefaultValue( currentVersion ); diff --git a/hibernate-core/src/main/java/org/hibernate/persister/entity/EntityPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/entity/EntityPersister.java index 809d38e08f..acc8510c9f 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/entity/EntityPersister.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/entity/EntityPersister.java @@ -1195,6 +1195,9 @@ public interface EntityPersister extends EntityMappingType, EntityMutationTarget /** * Set the identifier and version of the given instance back to its "unsaved" * value, that is, the value it had before it was made persistent. + * + * @see org.hibernate.cfg.AvailableSettings#USE_IDENTIFIER_ROLLBACK + * @see org.hibernate.boot.spi.SessionFactoryOptions#isIdentifierRollbackEnabled */ void resetIdentifier(Object entity, Object currentId, Object currentVersion, SharedSessionContractImplementor session);