version reset should not be affected by the id being assigned or generated

improve javadoc for this stuff

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-11-05 13:38:49 +01:00
parent b948fde3b3
commit 00f6115b42
5 changed files with 15 additions and 9 deletions

View File

@ -131,6 +131,11 @@ public interface SessionFactoryOptions extends QueryEngineOptions {
BaselineSessionEventsListenerBuilder getBaselineSessionEventsListenerBuilder(); BaselineSessionEventsListenerBuilder getBaselineSessionEventsListenerBuilder();
/**
* Should generated identifiers be reset after entity removal?
*
* @see org.hibernate.cfg.AvailableSettings#USE_IDENTIFIER_ROLLBACK
*/
boolean isIdentifierRollbackEnabled(); boolean isIdentifierRollbackEnabled();
boolean isCheckNullability(); boolean isCheckNullability();

View File

@ -170,6 +170,7 @@ public interface AvailableSettings
/** /**
* When enabled, specifies that the generated identifier of an entity is unset * When enabled, specifies that the generated identifier of an entity is unset
* when the entity is {@linkplain org.hibernate.Session#remove(Object) deleted}. * 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 * @settingDefault {@code false} - generated identifiers are not unset
* *

View File

@ -80,8 +80,8 @@ public class VersionValue implements UnsavedValueStrategy {
if ( version == null ) { if ( version == null ) {
return Boolean.TRUE; return Boolean.TRUE;
} }
if ( version instanceof Number ) { if ( version instanceof Number number ) {
return ((Number) version).longValue() < 0L; return number.longValue() < 0L;
} }
throw new MappingException( "unsaved-value NEGATIVE may only be used with short, int and long types" ); throw new MappingException( "unsaved-value NEGATIVE may only be used with short, int and long types" );
} }

View File

@ -4359,14 +4359,11 @@ public abstract class AbstractEntityPersister
Object currentId, Object currentId,
Object currentVersion, Object currentVersion,
SharedSessionContractImplementor session) { SharedSessionContractImplementor session) {
if ( entityMetamodel.getIdentifierProperty().getGenerator().allowAssignedIdentifiers() ) { if ( !getGenerator().allowAssignedIdentifiers() ) {
return;
}
// reset the identifier // reset the identifier
final Object defaultIdentifier = identifierMapping.getUnsavedStrategy().getDefaultValue( currentId ); final Object defaultIdentifier = identifierMapping.getUnsavedStrategy().getDefaultValue( currentId );
setIdentifier( entity, defaultIdentifier, session ); setIdentifier( entity, defaultIdentifier, session );
}
// reset the version // reset the version
if ( versionMapping != null ) { if ( versionMapping != null ) {
final Object defaultVersion = versionMapping.getUnsavedStrategy().getDefaultValue( currentVersion ); final Object defaultVersion = versionMapping.getUnsavedStrategy().getDefaultValue( currentVersion );

View File

@ -1195,6 +1195,9 @@ public interface EntityPersister extends EntityMappingType, EntityMutationTarget
/** /**
* Set the identifier and version of the given instance back to its "unsaved" * 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. * 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); void resetIdentifier(Object entity, Object currentId, Object currentVersion, SharedSessionContractImplementor session);