Skip to content

Commit 00f6115

Browse files
committed
version reset should not be affected by the id being assigned or generated
improve javadoc for this stuff Signed-off-by: Gavin King <[email protected]>
1 parent b948fde commit 00f6115

File tree

5 files changed

+15
-9
lines changed

5 files changed

+15
-9
lines changed

hibernate-core/src/main/java/org/hibernate/boot/spi/SessionFactoryOptions.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ default boolean isAllowRefreshDetachedEntity() {
131131

132132
BaselineSessionEventsListenerBuilder getBaselineSessionEventsListenerBuilder();
133133

134+
/**
135+
* Should generated identifiers be reset after entity removal?
136+
*
137+
* @see org.hibernate.cfg.AvailableSettings#USE_IDENTIFIER_ROLLBACK
138+
*/
134139
boolean isIdentifierRollbackEnabled();
135140

136141
boolean isCheckNullability();

hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ public interface AvailableSettings
170170
/**
171171
* When enabled, specifies that the generated identifier of an entity is unset
172172
* when the entity is {@linkplain org.hibernate.Session#remove(Object) deleted}.
173+
* If the entity is versioned, the version is also reset to its default value.
173174
*
174175
* @settingDefault {@code false} - generated identifiers are not unset
175176
*

hibernate-core/src/main/java/org/hibernate/engine/spi/VersionValue.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ public Boolean isUnsaved(@Nullable Object version) throws MappingException {
8080
if ( version == null ) {
8181
return Boolean.TRUE;
8282
}
83-
if ( version instanceof Number ) {
84-
return ((Number) version).longValue() < 0L;
83+
if ( version instanceof Number number ) {
84+
return number.longValue() < 0L;
8585
}
8686
throw new MappingException( "unsaved-value NEGATIVE may only be used with short, int and long types" );
8787
}

hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4359,14 +4359,11 @@ public void resetIdentifier(
43594359
Object currentId,
43604360
Object currentVersion,
43614361
SharedSessionContractImplementor session) {
4362-
if ( entityMetamodel.getIdentifierProperty().getGenerator().allowAssignedIdentifiers() ) {
4363-
return;
4362+
if ( !getGenerator().allowAssignedIdentifiers() ) {
4363+
// reset the identifier
4364+
final Object defaultIdentifier = identifierMapping.getUnsavedStrategy().getDefaultValue( currentId );
4365+
setIdentifier( entity, defaultIdentifier, session );
43644366
}
4365-
4366-
// reset the identifier
4367-
final Object defaultIdentifier = identifierMapping.getUnsavedStrategy().getDefaultValue( currentId );
4368-
setIdentifier( entity, defaultIdentifier, session );
4369-
43704367
// reset the version
43714368
if ( versionMapping != null ) {
43724369
final Object defaultVersion = versionMapping.getUnsavedStrategy().getDefaultValue( currentVersion );

hibernate-core/src/main/java/org/hibernate/persister/entity/EntityPersister.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,9 @@ default Object getIdentifier(Object entity) {
11951195
/**
11961196
* Set the identifier and version of the given instance back to its "unsaved"
11971197
* value, that is, the value it had before it was made persistent.
1198+
*
1199+
* @see org.hibernate.cfg.AvailableSettings#USE_IDENTIFIER_ROLLBACK
1200+
* @see org.hibernate.boot.spi.SessionFactoryOptions#isIdentifierRollbackEnabled
11981201
*/
11991202
void resetIdentifier(Object entity, Object currentId, Object currentVersion, SharedSessionContractImplementor session);
12001203

0 commit comments

Comments
 (0)