From 2c3ac612dbaf76c42fda6526f59c3bf6a70ff9d3 Mon Sep 17 00:00:00 2001 From: Sanne Grinovero Date: Mon, 12 Dec 2022 22:45:37 +0000 Subject: [PATCH] HHH-15858 Cleanup of internal Session casts --- .../java/org/hibernate/SessionBuilder.java | 28 +++--- .../org/hibernate/SharedSessionBuilder.java | 26 +++--- .../spi/interceptor/EnhancementHelper.java | 2 +- .../spi/AbstractPersistentCollection.java | 2 +- .../spi/AbstractCurrentSessionContext.java | 3 +- .../spi/AbstractDelegatingSessionBuilder.java | 58 ++++++------- ...ctDelegatingSessionBuilderImplementor.java | 4 +- ...bstractDelegatingSharedSessionBuilder.java | 86 +++++++++---------- .../engine/spi/SessionBuilderImplementor.java | 2 +- .../spi/SessionFactoryDelegatingImpl.java | 4 +- .../engine/spi/SessionFactoryImplementor.java | 11 ++- .../internal/SessionFactoryImpl.java | 81 ++++++++--------- .../org/hibernate/internal/SessionImpl.java | 74 ++++++++++++---- .../proxy/AbstractLazyInitializer.java | 2 +- .../sql/ast/spi/AbstractSqlAstTranslator.java | 2 +- .../entity/AbstractEntityInitializer.java | 2 +- .../TestDelegatingSessionBuilder.java | 2 +- ...stDelegatingSessionBuilderImplementor.java | 4 +- .../TestDelegatingSharedSessionBuilder.java | 2 +- .../orm/junit/SessionFactoryExtension.java | 8 +- 20 files changed, 221 insertions(+), 182 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/SessionBuilder.java b/hibernate-core/src/main/java/org/hibernate/SessionBuilder.java index 7558fadcab..e9bae27f00 100644 --- a/hibernate-core/src/main/java/org/hibernate/SessionBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/SessionBuilder.java @@ -17,7 +17,7 @@ import org.hibernate.resource.jdbc.spi.StatementInspector; * * @author Steve Ebersole */ -public interface SessionBuilder { +public interface SessionBuilder { /** * Opens a session with the specified options. * @@ -32,7 +32,7 @@ public interface SessionBuilder { * * @return {@code this}, for method chaining */ - T interceptor(Interceptor interceptor); + SessionBuilder interceptor(Interceptor interceptor); /** * Signifies that no {@link Interceptor} should be used. @@ -45,7 +45,7 @@ public interface SessionBuilder { * * @return {@code this}, for method chaining */ - T noInterceptor(); + SessionBuilder noInterceptor(); /** * Applies the given {@link StatementInspector} to the session. @@ -54,7 +54,7 @@ public interface SessionBuilder { * * @return {@code this}, for method chaining */ - T statementInspector(StatementInspector statementInspector); + SessionBuilder statementInspector(StatementInspector statementInspector); /** * Adds a specific connection to the session options. @@ -63,7 +63,7 @@ public interface SessionBuilder { * * @return {@code this}, for method chaining */ - T connection(Connection connection); + SessionBuilder connection(Connection connection); /** * Signifies that the connection release mode from the original session @@ -73,7 +73,7 @@ public interface SessionBuilder { * * @return {@code this}, for method chaining */ - T connectionHandlingMode(PhysicalConnectionHandlingMode mode); + SessionBuilder connectionHandlingMode(PhysicalConnectionHandlingMode mode); /** * Should the session built automatically join in any ongoing JTA transactions. @@ -84,7 +84,7 @@ public interface SessionBuilder { * * @see jakarta.persistence.SynchronizationType#SYNCHRONIZED */ - T autoJoinTransactions(boolean autoJoinTransactions); + SessionBuilder autoJoinTransactions(boolean autoJoinTransactions); /** * Should the session be automatically cleared on a failed transaction? @@ -94,7 +94,7 @@ public interface SessionBuilder { * @return {@code this}, for method chaining */ @SuppressWarnings("UnusedReturnValue") - T autoClear(boolean autoClear); + SessionBuilder autoClear(boolean autoClear); /** * Specify the initial FlushMode to use for the opened Session @@ -105,7 +105,7 @@ public interface SessionBuilder { * * @see jakarta.persistence.PersistenceContextType */ - T flushMode(FlushMode flushMode); + SessionBuilder flushMode(FlushMode flushMode); /** * Define the tenant identifier to be associated with the opened session. @@ -114,7 +114,7 @@ public interface SessionBuilder { * * @return {@code this}, for method chaining */ - T tenantIdentifier(String tenantIdentifier); + SessionBuilder tenantIdentifier(String tenantIdentifier); /** * Add one or more {@link SessionEventListener} instances to the list of @@ -124,7 +124,7 @@ public interface SessionBuilder { * * @return {@code this}, for method chaining */ - T eventListeners(SessionEventListener... listeners); + SessionBuilder eventListeners(SessionEventListener... listeners); /** * Remove all listeners intended for the built session currently held here, @@ -132,9 +132,9 @@ public interface SessionBuilder { * * {@code this}, for method chaining */ - T clearEventListeners(); + SessionBuilder clearEventListeners(); - T jdbcTimeZone(TimeZone timeZone); + SessionBuilder jdbcTimeZone(TimeZone timeZone); /** * Should the session be automatically closed after transaction completion? @@ -145,5 +145,5 @@ public interface SessionBuilder { * * @see jakarta.persistence.PersistenceContextType */ - T autoClose(boolean autoClose); + SessionBuilder autoClose(boolean autoClose); } diff --git a/hibernate-core/src/main/java/org/hibernate/SharedSessionBuilder.java b/hibernate-core/src/main/java/org/hibernate/SharedSessionBuilder.java index 315110c8f6..4d188263ba 100644 --- a/hibernate-core/src/main/java/org/hibernate/SharedSessionBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/SharedSessionBuilder.java @@ -13,21 +13,21 @@ import java.sql.Connection; * * @author Steve Ebersole */ -public interface SharedSessionBuilder extends SessionBuilder { +public interface SharedSessionBuilder extends SessionBuilder { /** * Signifies that the connection from the original session should be used to create the new session. * * @return {@code this}, for method chaining */ - T connection(); + SharedSessionBuilder connection(); /** * Signifies the interceptor from the original session should be used to create the new session. * * @return {@code this}, for method chaining */ - T interceptor(); + SharedSessionBuilder interceptor(); /** * Signifies that the connection release mode from the original session should be used to create the new session. @@ -37,51 +37,51 @@ public interface SharedSessionBuilder extends Se * @deprecated use {@link #connectionHandlingMode} instead. */ @Deprecated(since = "6.0") - T connectionReleaseMode(); + SharedSessionBuilder connectionReleaseMode(); /** * Signifies that the connection release mode from the original session should be used to create the new session. * * @return {@code this}, for method chaining */ - T connectionHandlingMode(); + SharedSessionBuilder connectionHandlingMode(); /** * Signifies that the autoJoinTransaction flag from the original session should be used to create the new session. * * @return {@code this}, for method chaining */ - T autoJoinTransactions(); + SharedSessionBuilder autoJoinTransactions(); /** * Signifies that the FlushMode from the original session should be used to create the new session. * * @return {@code this}, for method chaining */ - T flushMode(); + SharedSessionBuilder flushMode(); /** * Signifies that the autoClose flag from the original session should be used to create the new session. * * @return {@code this}, for method chaining */ - T autoClose(); + SharedSessionBuilder autoClose(); // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // overrides to maintain binary compatibility @Override - T interceptor(Interceptor interceptor); + SharedSessionBuilder interceptor(Interceptor interceptor); @Override - T noInterceptor(); + SharedSessionBuilder noInterceptor(); @Override - T connection(Connection connection); + SharedSessionBuilder connection(Connection connection); @Override - T autoJoinTransactions(boolean autoJoinTransactions); + SharedSessionBuilder autoJoinTransactions(boolean autoJoinTransactions); @Override - T autoClose(boolean autoClose); + SharedSessionBuilder autoClose(boolean autoClose); } diff --git a/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/spi/interceptor/EnhancementHelper.java b/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/spi/interceptor/EnhancementHelper.java index 1d9bcfbb4d..b6adc7443c 100644 --- a/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/spi/interceptor/EnhancementHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/spi/interceptor/EnhancementHelper.java @@ -284,7 +284,7 @@ public class EnhancementHelper { } final SessionFactoryImplementor sf = SessionFactoryRegistry.INSTANCE.getSessionFactory( interceptor.getSessionFactoryUuid() ); - final SharedSessionContractImplementor session = (SharedSessionContractImplementor) sf.openSession(); + final SharedSessionContractImplementor session = sf.openSession(); session.getPersistenceContextInternal().setDefaultReadOnly( true ); session.setHibernateFlushMode( FlushMode.MANUAL ); return session; diff --git a/hibernate-core/src/main/java/org/hibernate/collection/spi/AbstractPersistentCollection.java b/hibernate-core/src/main/java/org/hibernate/collection/spi/AbstractPersistentCollection.java index 0aceaf1f35..5ba86a637d 100644 --- a/hibernate-core/src/main/java/org/hibernate/collection/spi/AbstractPersistentCollection.java +++ b/hibernate-core/src/main/java/org/hibernate/collection/spi/AbstractPersistentCollection.java @@ -299,7 +299,7 @@ public abstract class AbstractPersistentCollection implements Serializable, P } final SessionFactoryImplementor sf = SessionFactoryRegistry.INSTANCE.getSessionFactory( sessionFactoryUuid ); - final SharedSessionContractImplementor session = (SharedSessionContractImplementor) sf.openSession(); + final SharedSessionContractImplementor session = sf.openSession(); session.getPersistenceContextInternal().setDefaultReadOnly( true ); session.setHibernateFlushMode( FlushMode.MANUAL ); return session; diff --git a/hibernate-core/src/main/java/org/hibernate/context/spi/AbstractCurrentSessionContext.java b/hibernate-core/src/main/java/org/hibernate/context/spi/AbstractCurrentSessionContext.java index 75b223ab12..0056c76963 100644 --- a/hibernate-core/src/main/java/org/hibernate/context/spi/AbstractCurrentSessionContext.java +++ b/hibernate-core/src/main/java/org/hibernate/context/spi/AbstractCurrentSessionContext.java @@ -11,6 +11,7 @@ import java.util.Objects; import org.hibernate.Session; import org.hibernate.SessionBuilder; import org.hibernate.context.TenantIdentifierMismatchException; +import org.hibernate.engine.spi.SessionBuilderImplementor; import org.hibernate.engine.spi.SessionFactoryImplementor; /** @@ -35,7 +36,7 @@ public abstract class AbstractCurrentSessionContext implements CurrentSessionCon } protected SessionBuilder baseSessionBuilder() { - final SessionBuilder builder = factory.withOptions(); + final SessionBuilderImplementor builder = factory.withOptions(); final CurrentTenantIdentifierResolver resolver = factory.getCurrentTenantIdentifierResolver(); if ( resolver != null ) { builder.tenantIdentifier( resolver.resolveCurrentTenantIdentifier() ); diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/AbstractDelegatingSessionBuilder.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/AbstractDelegatingSessionBuilder.java index 224dab17d0..e109d21f71 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/AbstractDelegatingSessionBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/AbstractDelegatingSessionBuilder.java @@ -24,7 +24,7 @@ import org.hibernate.resource.jdbc.spi.StatementInspector; * @author Gunnar Morling * @author Guillaume Smet */ -public abstract class AbstractDelegatingSessionBuilder implements SessionBuilder { +public abstract class AbstractDelegatingSessionBuilder implements SessionBuilder { private final SessionBuilder delegate; @@ -33,8 +33,8 @@ public abstract class AbstractDelegatingSessionBuilder } @SuppressWarnings("unchecked") - protected T getThis() { - return (T) this; + protected SessionBuilder getThis() { + return this; } protected SessionBuilder delegate() { @@ -47,80 +47,80 @@ public abstract class AbstractDelegatingSessionBuilder } @Override - public T interceptor(Interceptor interceptor) { + public SessionBuilder interceptor(Interceptor interceptor) { delegate.interceptor( interceptor ); - return getThis(); + return this; } @Override - public T noInterceptor() { + public SessionBuilder noInterceptor() { delegate.noInterceptor(); - return getThis(); + return this; } @Override - public T statementInspector(StatementInspector statementInspector) { + public SessionBuilder statementInspector(StatementInspector statementInspector) { delegate.statementInspector( statementInspector ); - return getThis(); + return this; } @Override - public T connection(Connection connection) { + public SessionBuilder connection(Connection connection) { delegate.connection( connection ); - return getThis(); + return this; } @Override - public T autoJoinTransactions(boolean autoJoinTransactions) { + public SessionBuilder autoJoinTransactions(boolean autoJoinTransactions) { delegate.autoJoinTransactions( autoJoinTransactions ); - return getThis(); + return this; } @Override - public T autoClose(boolean autoClose) { + public SessionBuilder autoClose(boolean autoClose) { delegate.autoClose( autoClose ); - return getThis(); + return this; } @Override - public T tenantIdentifier(String tenantIdentifier) { + public SessionBuilder tenantIdentifier(String tenantIdentifier) { delegate.tenantIdentifier( tenantIdentifier ); - return getThis(); + return this; } @Override - public T eventListeners(SessionEventListener... listeners) { + public SessionBuilder eventListeners(SessionEventListener... listeners) { delegate.eventListeners( listeners ); - return getThis(); + return this; } @Override - public T clearEventListeners() { + public SessionBuilder clearEventListeners() { delegate.clearEventListeners(); - return getThis(); + return this; } @Override - public T jdbcTimeZone(TimeZone timeZone) { + public SessionBuilder jdbcTimeZone(TimeZone timeZone) { delegate.jdbcTimeZone(timeZone); - return getThis(); + return this; } @Override - public T connectionHandlingMode(PhysicalConnectionHandlingMode mode) { + public SessionBuilder connectionHandlingMode(PhysicalConnectionHandlingMode mode) { delegate.connectionHandlingMode( mode ); - return getThis(); + return this; } @Override - public T autoClear(boolean autoClear) { + public SessionBuilder autoClear(boolean autoClear) { delegate.autoClear( autoClear ); - return getThis(); + return this; } @Override - public T flushMode(FlushMode flushMode) { + public SessionBuilder flushMode(FlushMode flushMode) { delegate.flushMode( flushMode ); - return getThis(); + return this; } } diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/AbstractDelegatingSessionBuilderImplementor.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/AbstractDelegatingSessionBuilderImplementor.java index 8532c22c44..e1b9ab480b 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/AbstractDelegatingSessionBuilderImplementor.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/AbstractDelegatingSessionBuilderImplementor.java @@ -12,9 +12,7 @@ package org.hibernate.engine.spi; * * @author Gunnar Morling */ -public abstract class AbstractDelegatingSessionBuilderImplementor - extends AbstractDelegatingSessionBuilder - implements SessionBuilderImplementor { +public abstract class AbstractDelegatingSessionBuilderImplementor extends AbstractDelegatingSessionBuilder implements SessionBuilderImplementor { public AbstractDelegatingSessionBuilderImplementor(SessionBuilderImplementor delegate) { super( delegate ); diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/AbstractDelegatingSharedSessionBuilder.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/AbstractDelegatingSharedSessionBuilder.java index 25f97d19e8..d67f327f7b 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/AbstractDelegatingSharedSessionBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/AbstractDelegatingSharedSessionBuilder.java @@ -24,7 +24,7 @@ import org.hibernate.resource.jdbc.spi.StatementInspector; * @author Gunnar Morling * @author Guillaume Smet */ -public abstract class AbstractDelegatingSharedSessionBuilder implements SharedSessionBuilder { +public abstract class AbstractDelegatingSharedSessionBuilder implements SharedSessionBuilder { private final SharedSessionBuilder delegate; @@ -33,8 +33,8 @@ public abstract class AbstractDelegatingSharedSessionBuilder extends SessionBuilder { +public interface SessionBuilderImplementor extends SessionBuilder { } diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionFactoryDelegatingImpl.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionFactoryDelegatingImpl.java index 23214462b9..65602386e3 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionFactoryDelegatingImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionFactoryDelegatingImpl.java @@ -80,7 +80,7 @@ public class SessionFactoryDelegatingImpl implements SessionFactoryImplementor, } @Override - public Session openSession() throws HibernateException { + public SessionImplementor openSession() throws HibernateException { return delegate.openSession(); } @@ -215,7 +215,7 @@ public class SessionFactoryDelegatingImpl implements SessionFactoryImplementor, } @Override - public Session openTemporarySession() throws HibernateException { + public SessionImplementor openTemporarySession() throws HibernateException { return delegate.openTemporarySession(); } diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionFactoryImplementor.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionFactoryImplementor.java index 32eb8cc011..92202737c9 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionFactoryImplementor.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionFactoryImplementor.java @@ -67,6 +67,15 @@ public interface SessionFactoryImplementor */ String getName(); + /** + * Overrides {@link SessionFactory#openSession()} to widen the return type: + * this is useful for internal code depending on {@link SessionFactoryImplementor} + * as it would otherwise need to frequently resort to casting to the internal contract. + * @return the opened Session. + */ + @Override + SessionImplementor openSession(); + TypeConfiguration getTypeConfiguration(); default SessionFactoryImplementor getSessionFactory() { @@ -89,7 +98,7 @@ public interface SessionFactoryImplementor /** * Get a non-transactional "current" session (used by hibernate-envers) */ - Session openTemporarySession() throws HibernateException; + SessionImplementor openTemporarySession() throws HibernateException; @Override CacheImplementor getCache(); diff --git a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java index b49db53d71..d9dc3f2276 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java @@ -40,7 +40,6 @@ import org.hibernate.HibernateException; import org.hibernate.Interceptor; import org.hibernate.MappingException; import org.hibernate.Session; -import org.hibernate.SessionBuilder; import org.hibernate.SessionEventListener; import org.hibernate.SessionFactory; import org.hibernate.SessionFactoryObserver; @@ -75,6 +74,7 @@ import org.hibernate.engine.profile.FetchProfile; import org.hibernate.engine.spi.FilterDefinition; import org.hibernate.engine.spi.SessionBuilderImplementor; import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform; import org.hibernate.event.spi.EventEngine; import org.hibernate.graph.spi.RootGraphImplementor; @@ -132,7 +132,6 @@ import org.jboss.logging.Logger; import static java.util.Collections.unmodifiableSet; - /** * Concrete implementation of the {@code SessionFactory} interface. Has the following * responsibilities @@ -189,8 +188,8 @@ public class SessionFactoryImpl implements SessionFactoryImplementor { private final transient FastSessionServices fastSessionServices; private final transient WrapperOptions wrapperOptions; - private final transient SessionBuilder defaultSessionOpenOptions; - private final transient SessionBuilder temporarySessionOpenOptions; + private final transient SessionBuilderImpl defaultSessionOpenOptions; + private final transient SessionBuilderImpl temporarySessionOpenOptions; private final transient StatelessSessionBuilder defaultStatelessOptions; private final transient EntityNameResolver entityNameResolver; @@ -421,7 +420,7 @@ public class SessionFactoryImpl implements SessionFactoryImplementor { this.schemaManager = new SchemaManagerImpl( this, bootMetamodel ); } - private SessionBuilder createDefaultSessionOpenOptionsIfPossible() { + private SessionBuilderImpl createDefaultSessionOpenOptionsIfPossible() { final CurrentTenantIdentifierResolver currentTenantIdentifierResolver = getCurrentTenantIdentifierResolver(); if ( currentTenantIdentifierResolver == null ) { return withOptions(); @@ -432,7 +431,7 @@ public class SessionFactoryImpl implements SessionFactoryImplementor { } } - private SessionBuilder buildTemporarySessionOpenOptions() { + private SessionBuilderImpl buildTemporarySessionOpenOptions() { return withOptions() .autoClose( false ) .flushMode( FlushMode.MANUAL ) @@ -493,7 +492,7 @@ public class SessionFactoryImpl implements SessionFactoryImplementor { getCache().prime( regionConfigs ); } - public Session openSession() throws HibernateException { + public SessionImplementor openSession() throws HibernateException { //The defaultSessionOpenOptions can't be used in some cases; for example when using a TenantIdentifierResolver. if ( this.defaultSessionOpenOptions != null ) { return this.defaultSessionOpenOptions.openSession(); @@ -503,7 +502,8 @@ public class SessionFactoryImpl implements SessionFactoryImplementor { } } - public Session openTemporarySession() throws HibernateException { + @Override + public SessionImpl openTemporarySession() throws HibernateException { //The temporarySessionOpenOptions can't be used in some cases; for example when using a TenantIdentifierResolver. if ( this.temporarySessionOpenOptions != null ) { return this.temporarySessionOpenOptions.openSession(); @@ -522,8 +522,8 @@ public class SessionFactoryImpl implements SessionFactoryImplementor { } @Override - public SessionBuilderImplementor withOptions() { - return new SessionBuilderImpl<>( this ); + public SessionBuilderImpl withOptions() { + return new SessionBuilderImpl( this ); } @Override @@ -1149,7 +1149,7 @@ public class SessionFactoryImpl implements SessionFactoryImplementor { return null; } - public static class SessionBuilderImpl implements SessionBuilderImplementor, SessionCreationOptions { + public static class SessionBuilderImpl implements SessionBuilderImplementor, SessionCreationOptions { private static final Logger log = CoreLogging.logger( SessionBuilderImpl.class ); private final SessionFactoryImpl sessionFactory; @@ -1259,91 +1259,86 @@ public class SessionFactoryImpl implements SessionFactoryImplementor { // SessionBuilder @Override - public Session openSession() { + public SessionImpl openSession() { log.tracef( "Opening Hibernate Session. tenant=%s", tenantIdentifier ); return new SessionImpl( sessionFactory, this ); } - @SuppressWarnings("unchecked") - private T getThis() { - return (T) this; - } - @Override - public T interceptor(Interceptor interceptor) { + public SessionBuilderImpl interceptor(Interceptor interceptor) { this.interceptor = interceptor; this.explicitNoInterceptor = false; - return getThis(); + return this; } @Override - public T noInterceptor() { + public SessionBuilderImpl noInterceptor() { this.interceptor = EmptyInterceptor.INSTANCE; this.explicitNoInterceptor = true; - return getThis(); + return this; } @Override - public T statementInspector(StatementInspector statementInspector) { + public SessionBuilderImpl statementInspector(StatementInspector statementInspector) { this.statementInspector = statementInspector; - return getThis(); + return this; } @Override - public T connection(Connection connection) { + public SessionBuilderImpl connection(Connection connection) { this.connection = connection; - return getThis(); + return this; } @Override - public T connectionHandlingMode(PhysicalConnectionHandlingMode connectionHandlingMode) { + public SessionBuilderImpl connectionHandlingMode(PhysicalConnectionHandlingMode connectionHandlingMode) { this.connectionHandlingMode = connectionHandlingMode; - return getThis(); + return this; } @Override - public T autoJoinTransactions(boolean autoJoinTransactions) { + public SessionBuilderImpl autoJoinTransactions(boolean autoJoinTransactions) { this.autoJoinTransactions = autoJoinTransactions; - return getThis(); + return this; } @Override - public T autoClose(boolean autoClose) { + public SessionBuilderImpl autoClose(boolean autoClose) { this.autoClose = autoClose; - return getThis(); + return this; } @Override - public T autoClear(boolean autoClear) { + public SessionBuilderImpl autoClear(boolean autoClear) { this.autoClear = autoClear; - return getThis(); + return this; } @Override - public T flushMode(FlushMode flushMode) { + public SessionBuilderImpl flushMode(FlushMode flushMode) { this.flushMode = flushMode; - return getThis(); + return this; } @Override - public T tenantIdentifier(String tenantIdentifier) { + public SessionBuilderImpl tenantIdentifier(String tenantIdentifier) { this.tenantIdentifier = tenantIdentifier; - return getThis(); + return this; } @Override - public T eventListeners(SessionEventListener... listeners) { + public SessionBuilderImpl eventListeners(SessionEventListener... listeners) { if ( this.listeners == null ) { this.listeners = sessionFactory.getSessionFactoryOptions() .getBaselineSessionEventsListenerBuilder() .buildBaselineList(); } Collections.addAll( this.listeners, listeners ); - return getThis(); + return this; } @Override - public T clearEventListeners() { + public SessionBuilderImpl clearEventListeners() { if ( listeners == null ) { //Needs to initialize explicitly to an empty list as otherwise "null" implies the default listeners will be applied this.listeners = new ArrayList<>( 3 ); @@ -1351,13 +1346,13 @@ public class SessionFactoryImpl implements SessionFactoryImplementor { else { listeners.clear(); } - return getThis(); + return this; } @Override - public T jdbcTimeZone(TimeZone timeZone) { + public SessionBuilderImpl jdbcTimeZone(TimeZone timeZone) { jdbcTimeZone = timeZone; - return getThis(); + return this; } } diff --git a/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java index f3773a6877..425d38f06a 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java @@ -14,6 +14,7 @@ import java.io.Reader; import java.io.Serializable; import java.sql.Blob; import java.sql.Clob; +import java.sql.Connection; import java.sql.NClob; import java.sql.SQLException; import java.util.Collections; @@ -30,6 +31,7 @@ import org.hibernate.Filter; import org.hibernate.FlushMode; import org.hibernate.HibernateException; import org.hibernate.IdentifierLoadAccess; +import org.hibernate.Interceptor; import org.hibernate.JDBCException; import org.hibernate.LobHelper; import org.hibernate.LockMode; @@ -344,7 +346,7 @@ public class SessionImpl @Override public SharedSessionBuilder sessionWithOptions() { - return new SharedSessionBuilderImpl<>( this ); + return new SharedSessionBuilderImpl( this ); } @Override @@ -2019,9 +2021,7 @@ public class SessionImpl } } - private static class SharedSessionBuilderImpl - extends SessionFactoryImpl.SessionBuilderImpl - implements SharedSessionBuilder, SharedSessionCreationOptions { + private static class SharedSessionBuilderImpl extends SessionFactoryImpl.SessionBuilderImpl implements SharedSessionBuilder, SharedSessionCreationOptions { private final SessionImpl session; private boolean shareTransactionContext; @@ -2036,51 +2036,87 @@ public class SessionImpl @Override - public T tenantIdentifier(String tenantIdentifier) { + public SharedSessionBuilderImpl tenantIdentifier(String tenantIdentifier) { // todo : is this always true? Or just in the case of sharing JDBC resources? throw new SessionException( "Cannot redefine tenant identifier on child session" ); } @Override - public T interceptor() { - return interceptor( session.getInterceptor() ); + public SharedSessionBuilderImpl interceptor() { + super.interceptor( session.getInterceptor() ); + return this; + } + + @Override + public SharedSessionBuilderImpl interceptor(Interceptor interceptor) { + super.interceptor( interceptor ); + return this; + } + + @Override + public SharedSessionBuilderImpl noInterceptor() { + super.noInterceptor(); + return this; } @Override @SuppressWarnings("unchecked") - public T connection() { + public SharedSessionBuilderImpl connection() { this.shareTransactionContext = true; - return (T) this; + return this; + } + + @Override + public SharedSessionBuilderImpl connection(Connection connection) { + super.connection( connection ); + return this; } @Override @Deprecated(since = "6.0") - public T connectionReleaseMode() { + public SharedSessionBuilderImpl connectionReleaseMode() { final PhysicalConnectionHandlingMode handlingMode = PhysicalConnectionHandlingMode.interpret( ConnectionAcquisitionMode.AS_NEEDED, session.getJdbcCoordinator().getLogicalConnection().getConnectionHandlingMode().getReleaseMode() ); - return connectionHandlingMode( handlingMode ); + connectionHandlingMode( handlingMode ); + return this; } @Override - public T connectionHandlingMode() { - return connectionHandlingMode( session.getJdbcCoordinator().getLogicalConnection().getConnectionHandlingMode() ); + public SharedSessionBuilderImpl connectionHandlingMode() { + connectionHandlingMode( session.getJdbcCoordinator().getLogicalConnection().getConnectionHandlingMode() ); + return this; } @Override - public T autoJoinTransactions() { - return autoJoinTransactions( session.isAutoCloseSessionEnabled() ); + public SharedSessionBuilderImpl autoJoinTransactions() { + super.autoJoinTransactions( session.isAutoCloseSessionEnabled() ); + return this; } @Override - public T flushMode() { - return flushMode( session.getHibernateFlushMode() ); + public SharedSessionBuilderImpl autoJoinTransactions(boolean autoJoinTransactions) { + super.autoJoinTransactions( autoJoinTransactions ); + return this; } @Override - public T autoClose() { - return autoClose( session.autoClose ); + public SharedSessionBuilderImpl autoClose(boolean autoClose) { + super.autoClose( autoClose ); + return this; + } + + @Override + public SharedSessionBuilderImpl flushMode() { + flushMode( session.getHibernateFlushMode() ); + return this; + } + + @Override + public SharedSessionBuilderImpl autoClose() { + autoClose( session.autoClose ); + return this; } // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/hibernate-core/src/main/java/org/hibernate/proxy/AbstractLazyInitializer.java b/hibernate-core/src/main/java/org/hibernate/proxy/AbstractLazyInitializer.java index 0b5194d7c0..65a3d8af15 100644 --- a/hibernate-core/src/main/java/org/hibernate/proxy/AbstractLazyInitializer.java +++ b/hibernate-core/src/main/java/org/hibernate/proxy/AbstractLazyInitializer.java @@ -194,7 +194,7 @@ public abstract class AbstractLazyInitializer implements LazyInitializer { } try { SessionFactoryImplementor sf = SessionFactoryRegistry.INSTANCE.getSessionFactory( sessionFactoryUuid ); - SharedSessionContractImplementor session = (SharedSessionContractImplementor) sf.openSession(); + SharedSessionContractImplementor session = sf.openSession(); session.getPersistenceContext().setDefaultReadOnly( true ); session.setHibernateFlushMode( FlushMode.MANUAL ); diff --git a/hibernate-core/src/main/java/org/hibernate/sql/ast/spi/AbstractSqlAstTranslator.java b/hibernate-core/src/main/java/org/hibernate/sql/ast/spi/AbstractSqlAstTranslator.java index 4e78d73801..5fd19fab37 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/ast/spi/AbstractSqlAstTranslator.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/ast/spi/AbstractSqlAstTranslator.java @@ -385,7 +385,7 @@ public abstract class AbstractSqlAstTranslator implemen @Override protected SessionImplementor delegate() { if ( session == null ) { - session = (SessionImplementor) sessionFactory.openTemporarySession(); + session = sessionFactory.openTemporarySession(); } return session; } diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/AbstractEntityInitializer.java b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/AbstractEntityInitializer.java index fb052a0232..ecd21df197 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/AbstractEntityInitializer.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/AbstractEntityInitializer.java @@ -969,7 +969,7 @@ public abstract class AbstractEntityInitializer extends AbstractFetchParentAcces private void preLoad(RowProcessingState rowProcessingState) { final SharedSessionContractImplementor session = rowProcessingState.getJdbcValuesSourceProcessingState().getSession(); - if ( session instanceof EventSource ) { + if ( session.isEventSource() ) { final PreLoadEvent preLoadEvent = rowProcessingState.getJdbcValuesSourceProcessingState().getPreLoadEvent(); assert preLoadEvent != null; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/delegation/TestDelegatingSessionBuilder.java b/hibernate-core/src/test/java/org/hibernate/orm/test/delegation/TestDelegatingSessionBuilder.java index b601c5dbf0..3c6bae0566 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/delegation/TestDelegatingSessionBuilder.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/delegation/TestDelegatingSessionBuilder.java @@ -19,7 +19,7 @@ import org.hibernate.engine.spi.AbstractDelegatingSessionBuilder; * @author Guillaume Smet */ @SuppressWarnings("unused") -public class TestDelegatingSessionBuilder extends AbstractDelegatingSessionBuilder { +public class TestDelegatingSessionBuilder extends AbstractDelegatingSessionBuilder { @SuppressWarnings("rawtypes") public TestDelegatingSessionBuilder(SessionBuilder delegate) { diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/delegation/TestDelegatingSessionBuilderImplementor.java b/hibernate-core/src/test/java/org/hibernate/orm/test/delegation/TestDelegatingSessionBuilderImplementor.java index c0e8da3f8b..ec4cc454cb 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/delegation/TestDelegatingSessionBuilderImplementor.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/delegation/TestDelegatingSessionBuilderImplementor.java @@ -19,9 +19,9 @@ import org.hibernate.engine.spi.SessionBuilderImplementor; * @author Guillaume Smet */ @SuppressWarnings("unused") -public class TestDelegatingSessionBuilderImplementor extends AbstractDelegatingSessionBuilderImplementor { +public class TestDelegatingSessionBuilderImplementor extends AbstractDelegatingSessionBuilderImplementor { - public TestDelegatingSessionBuilderImplementor(SessionBuilderImplementor delegate) { + public TestDelegatingSessionBuilderImplementor(SessionBuilderImplementor delegate) { super( delegate ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/delegation/TestDelegatingSharedSessionBuilder.java b/hibernate-core/src/test/java/org/hibernate/orm/test/delegation/TestDelegatingSharedSessionBuilder.java index c97bed6192..5c53c65386 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/delegation/TestDelegatingSharedSessionBuilder.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/delegation/TestDelegatingSharedSessionBuilder.java @@ -20,7 +20,7 @@ import org.hibernate.engine.spi.AbstractDelegatingSharedSessionBuilder; * @author Guillaume Smet */ @SuppressWarnings("unused") -public class TestDelegatingSharedSessionBuilder extends AbstractDelegatingSharedSessionBuilder { +public class TestDelegatingSharedSessionBuilder extends AbstractDelegatingSharedSessionBuilder { @SuppressWarnings("rawtypes") public TestDelegatingSharedSessionBuilder(SharedSessionBuilder delegate) { diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/SessionFactoryExtension.java b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/SessionFactoryExtension.java index c777fd5ebd..0fc39435fc 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/SessionFactoryExtension.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/SessionFactoryExtension.java @@ -321,7 +321,7 @@ public class SessionFactoryExtension public void inSession(Consumer action) { log.trace( "#inSession(Consumer)" ); - try (SessionImplementor session = (SessionImplementor) getSessionFactory().openSession()) { + try (SessionImplementor session = getSessionFactory().openSession()) { log.trace( "Session opened, calling action" ); action.accept( session ); } @@ -334,7 +334,7 @@ public class SessionFactoryExtension public T fromSession(Function action) { log.trace( "#fromSession(Function)" ); - try (SessionImplementor session = (SessionImplementor) getSessionFactory().openSession()) { + try (SessionImplementor session = getSessionFactory().openSession()) { log.trace( "Session opened, calling action" ); return action.apply( session ); } @@ -347,7 +347,7 @@ public class SessionFactoryExtension public void inTransaction(Consumer action) { log.trace( "#inTransaction(Consumer)" ); - try (SessionImplementor session = (SessionImplementor) getSessionFactory().openSession()) { + try (SessionImplementor session = getSessionFactory().openSession()) { log.trace( "Session opened, calling action" ); inTransaction( session, action ); } @@ -360,7 +360,7 @@ public class SessionFactoryExtension public T fromTransaction(Function action) { log.trace( "#fromTransaction(Function)" ); - try (SessionImplementor session = (SessionImplementor) getSessionFactory().openSession()) { + try (SessionImplementor session = getSessionFactory().openSession()) { log.trace( "Session opened, calling action" ); return fromTransaction( session, action ); }