Skip to content

Commit d9571e7

Browse files
committed
introduce SessionBuilder.identifierRollback()
allow identifier rollback to be requested via API
1 parent 8e6b576 commit d9571e7

11 files changed

+93
-20
lines changed

hibernate-core/src/main/java/org/hibernate/SessionBuilder.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ public interface SessionBuilder {
9696
SessionBuilder autoClear(boolean autoClear);
9797

9898
/**
99-
* Specify the initial FlushMode to use for the opened Session
99+
* Specify the initial {@link FlushMode} to use for the opened Session
100100
*
101-
* @param flushMode The initial FlushMode to use for the opened Session
101+
* @param flushMode The initial {@code FlushMode} to use for the opened Session
102102
*
103103
* @return {@code this}, for method chaining
104104
*
@@ -145,6 +145,12 @@ public interface SessionBuilder {
145145
*/
146146
SessionBuilder clearEventListeners();
147147

148+
/**
149+
* Specify the {@linkplain org.hibernate.cfg.JdbcSettings#JDBC_TIME_ZONE
150+
* JDBC time zone} for the session.
151+
*
152+
* @return {@code this}, for method chaining
153+
*/
148154
SessionBuilder jdbcTimeZone(TimeZone timeZone);
149155

150156
/**
@@ -157,4 +163,13 @@ public interface SessionBuilder {
157163
* @see jakarta.persistence.PersistenceContextType
158164
*/
159165
SessionBuilder autoClose(boolean autoClose);
166+
167+
/**
168+
* Enable identifier rollback after entity removal for the session.
169+
*
170+
* @return {@code this}, for method chaining
171+
*
172+
* @see org.hibernate.cfg.AvailableSettings#USE_IDENTIFIER_ROLLBACK
173+
*/
174+
SessionBuilder identifierRollback(boolean identifierRollback);
160175
}

hibernate-core/src/main/java/org/hibernate/SharedSessionBuilder.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,7 @@ public interface SharedSessionBuilder extends SessionBuilder {
112112

113113
@Override
114114
SharedSessionBuilder autoClose(boolean autoClose);
115+
116+
@Override
117+
SharedSessionBuilder identifierRollback(boolean identifierRollback);
115118
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,10 @@ public SessionBuilder flushMode(FlushMode flushMode) {
126126
delegate.flushMode( flushMode );
127127
return this;
128128
}
129+
130+
@Override
131+
public SessionBuilder identifierRollback(boolean identifierRollback) {
132+
delegate.identifierRollback( identifierRollback );
133+
return this;
134+
}
129135
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,10 @@ public SharedSessionBuilder jdbcTimeZone(TimeZone timeZone) {
168168
delegate.jdbcTimeZone( timeZone );
169169
return this;
170170
}
171+
172+
@Override
173+
public SharedSessionBuilder identifierRollback(boolean identifierRollback) {
174+
delegate.identifierRollback( identifierRollback );
175+
return this;
176+
}
171177
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,4 +1241,9 @@ public FormatMapper getXmlFormatMapper() {
12411241
public Object loadFromSecondLevelCache(EntityPersister persister, EntityKey entityKey, Object instanceToLoad, LockMode lockMode) {
12421242
return delegate.loadFromSecondLevelCache( persister, entityKey, instanceToLoad, lockMode );
12431243
}
1244+
1245+
@Override
1246+
public boolean isIdentifierRollbackEnabled() {
1247+
return delegate.isIdentifierRollbackEnabled();
1248+
}
12441249
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,8 @@ default String bestGuessEntityName(Object object, EntityEntry entry) {
377377
*/
378378
boolean isDefaultReadOnly();
379379

380+
boolean isIdentifierRollbackEnabled();
381+
380382
void setCriteriaCopyTreeEnabled(boolean jpaCriteriaCopyComplianceEnabled);
381383

382384
boolean isCriteriaCopyTreeEnabled();

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,4 +703,9 @@ public void lock(String entityName, Object child, LockOptions lockOptions) {
703703
public Object loadFromSecondLevelCache(EntityPersister persister, EntityKey entityKey, Object instanceToLoad, LockMode lockMode) {
704704
return delegate.loadFromSecondLevelCache( persister, entityKey, instanceToLoad, lockMode );
705705
}
706+
707+
@Override
708+
public boolean isIdentifierRollbackEnabled() {
709+
return delegate.isIdentifierRollbackEnabled();
710+
}
706711
}

hibernate-core/src/main/java/org/hibernate/internal/SessionCreationOptions.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public interface SessionCreationOptions {
4646

4747
Object getTenantIdentifierValue();
4848

49+
boolean isIdentifierRollbackEnabled();
50+
4951
TimeZone getJdbcTimeZone();
5052

5153
/**

hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import java.io.Serial;
1212
import java.sql.Connection;
1313
import java.util.ArrayList;
14-
import java.util.Collections;
1514
import java.util.HashMap;
1615
import java.util.HashSet;
1716
import java.util.List;
@@ -32,6 +31,7 @@
3231
import org.hibernate.HibernateException;
3332
import org.hibernate.Interceptor;
3433
import org.hibernate.Session;
34+
import org.hibernate.SessionBuilder;
3535
import org.hibernate.SessionEventListener;
3636
import org.hibernate.SessionFactory;
3737
import org.hibernate.SessionFactoryObserver;
@@ -78,7 +78,6 @@
7878
import org.hibernate.graph.spi.RootGraphImplementor;
7979
import org.hibernate.integrator.spi.Integrator;
8080
import org.hibernate.integrator.spi.IntegratorService;
81-
import org.hibernate.internal.util.collections.ArrayHelper;
8281
import org.hibernate.jpa.internal.ExceptionMapperLegacyJpaImpl;
8382
import org.hibernate.jpa.internal.PersistenceUnitUtilImpl;
8483
import org.hibernate.mapping.Collection;
@@ -567,12 +566,9 @@ public StatelessSessionBuilder withStatelessOptions() {
567566

568567
@Override
569568
public StatelessSession openStatelessSession() {
570-
if ( defaultStatelessOptions != null ) {
571-
return defaultStatelessOptions.openStatelessSession();
572-
}
573-
else {
574-
return withStatelessOptions().openStatelessSession();
575-
}
569+
return defaultStatelessOptions != null
570+
? defaultStatelessOptions.openStatelessSession()
571+
: withStatelessOptions().openStatelessSession();
576572
}
577573

578574
@Override
@@ -1129,6 +1125,7 @@ public static class SessionBuilderImpl implements SessionBuilderImplementor, Ses
11291125
private boolean autoClose;
11301126
private boolean autoClear;
11311127
private Object tenantIdentifier;
1128+
private boolean identifierRollback;
11321129
private TimeZone jdbcTimeZone;
11331130
private boolean explicitNoInterceptor;
11341131
private final int defaultBatchFetchSize;
@@ -1152,6 +1149,7 @@ public SessionBuilderImpl(SessionFactoryImpl sessionFactory) {
11521149
autoClose = sessionFactoryOptions.isAutoCloseSessionEnabled();
11531150
defaultBatchFetchSize = sessionFactoryOptions.getDefaultBatchFetchSize();
11541151
subselectFetchEnabled = sessionFactoryOptions.isSubselectFetchEnabled();
1152+
identifierRollback = sessionFactoryOptions.isIdentifierRollbackEnabled();
11551153

11561154
final CurrentTenantIdentifierResolver<Object> currentTenantIdentifierResolver =
11571155
sessionFactory.getCurrentTenantIdentifierResolver();
@@ -1235,6 +1233,11 @@ public Object getTenantIdentifierValue() {
12351233
return tenantIdentifier;
12361234
}
12371235

1236+
@Override
1237+
public boolean isIdentifierRollbackEnabled() {
1238+
return identifierRollback;
1239+
}
1240+
12381241
@Override
12391242
public TimeZone getJdbcTimeZone() {
12401243
return jdbcTimeZone;
@@ -1322,6 +1325,12 @@ public SessionBuilderImpl tenantIdentifier(Object tenantIdentifier) {
13221325
return this;
13231326
}
13241327

1328+
@Override
1329+
public SessionBuilder identifierRollback(boolean identifierRollback) {
1330+
this.identifierRollback = identifierRollback;
1331+
return this;
1332+
}
1333+
13251334
@Override
13261335
public SessionBuilderImpl eventListeners(SessionEventListener... listeners) {
13271336
if ( this.listeners == null ) {
@@ -1435,8 +1444,14 @@ public Connection getConnection() {
14351444

14361445
@Override
14371446
public Interceptor getInterceptor() {
1438-
return configuredInterceptor( EmptyInterceptor.INSTANCE, false, sessionFactory.getSessionFactoryOptions() );
1447+
return configuredInterceptor( EmptyInterceptor.INSTANCE, false,
1448+
sessionFactory.getSessionFactoryOptions() );
1449+
}
14391450

1451+
@Override
1452+
public boolean isIdentifierRollbackEnabled() {
1453+
// identifier rollback not yet implemented for StatelessSessions
1454+
return false;
14401455
}
14411456

14421457
@Override
@@ -1451,10 +1466,8 @@ public PhysicalConnectionHandlingMode getPhysicalConnectionHandlingMode() {
14511466

14521467
@Override
14531468
public String getTenantIdentifier() {
1454-
if ( tenantIdentifier == null ) {
1455-
return null;
1456-
}
1457-
return sessionFactory.getTenantIdentifierJavaType().toString( tenantIdentifier );
1469+
return tenantIdentifier == null ? null
1470+
: sessionFactory.getTenantIdentifierJavaType().toString( tenantIdentifier );
14581471
}
14591472

14601473
@Override

hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,8 @@ public class SessionImpl
250250
private boolean autoClear;
251251
private final boolean autoClose;
252252

253+
private final boolean identifierRollbackEnabled;
254+
253255
private transient LoadEvent loadEvent; //cached LoadEvent instance
254256
private transient PostLoadEvent postLoadEvent; //cached PostLoadEvent instance
255257

@@ -267,6 +269,8 @@ public SessionImpl(SessionFactoryImpl factory, SessionCreationOptions options) {
267269
autoClear = options.shouldAutoClear();
268270
autoClose = options.shouldAutoClose();
269271

272+
identifierRollbackEnabled = options.isIdentifierRollbackEnabled();
273+
270274
setUpTransactionCompletionProcesses( options );
271275

272276
loadQueryInfluencers = new LoadQueryInfluencers( factory, options );
@@ -505,6 +509,11 @@ public boolean isAutoCloseSessionEnabled() {
505509
return autoClose;
506510
}
507511

512+
@Override
513+
public boolean isIdentifierRollbackEnabled() {
514+
return identifierRollbackEnabled;
515+
}
516+
508517
@Override
509518
public boolean isOpen() {
510519
checkSessionFactoryOpen();
@@ -2115,6 +2124,7 @@ private SharedSessionBuilderImpl(SessionImpl session) {
21152124
super( (SessionFactoryImpl) session.getFactory() );
21162125
this.session = session;
21172126
super.tenantIdentifier( session.getTenantIdentifierValue() );
2127+
super.identifierRollback( session.isIdentifierRollbackEnabled() );
21182128
}
21192129

21202130
@Override
@@ -2222,6 +2232,12 @@ public SharedSessionBuilderImpl autoClose() {
22222232
return this;
22232233
}
22242234

2235+
@Override
2236+
public SharedSessionBuilderImpl identifierRollback(boolean identifierRollback) {
2237+
super.identifierRollback( identifierRollback );
2238+
return this;
2239+
}
2240+
22252241
@Override
22262242
public SharedSessionBuilderImpl jdbcTimeZone(TimeZone timeZone) {
22272243
super.jdbcTimeZone(timeZone);

0 commit comments

Comments
 (0)