HHH-15858 Cleanup of internal Session casts

This commit is contained in:
Sanne Grinovero 2022-12-12 22:45:37 +00:00 committed by Sanne Grinovero
parent 48c383121f
commit 2c3ac612db
20 changed files with 221 additions and 182 deletions

View File

@ -17,7 +17,7 @@ import org.hibernate.resource.jdbc.spi.StatementInspector;
*
* @author Steve Ebersole
*/
public interface SessionBuilder<T extends SessionBuilder> {
public interface SessionBuilder {
/**
* Opens a session with the specified options.
*
@ -32,7 +32,7 @@ public interface SessionBuilder<T extends 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<T extends 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<T extends 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<T extends 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<T extends 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<T extends 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<T extends 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<T extends 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<T extends 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<T extends 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<T extends 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<T extends SessionBuilder> {
*
* @see jakarta.persistence.PersistenceContextType
*/
T autoClose(boolean autoClose);
SessionBuilder autoClose(boolean autoClose);
}

View File

@ -13,21 +13,21 @@ import java.sql.Connection;
*
* @author Steve Ebersole
*/
public interface SharedSessionBuilder<T extends SharedSessionBuilder> extends SessionBuilder<T> {
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<T extends 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);
}

View File

@ -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;

View File

@ -299,7 +299,7 @@ public abstract class AbstractPersistentCollection<E> 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;

View File

@ -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() );

View File

@ -24,7 +24,7 @@ import org.hibernate.resource.jdbc.spi.StatementInspector;
* @author Gunnar Morling
* @author Guillaume Smet
*/
public abstract class AbstractDelegatingSessionBuilder<T extends SessionBuilder> implements SessionBuilder<T> {
public abstract class AbstractDelegatingSessionBuilder implements SessionBuilder {
private final SessionBuilder delegate;
@ -33,8 +33,8 @@ public abstract class AbstractDelegatingSessionBuilder<T extends SessionBuilder>
}
@SuppressWarnings("unchecked")
protected T getThis() {
return (T) this;
protected SessionBuilder getThis() {
return this;
}
protected SessionBuilder delegate() {
@ -47,80 +47,80 @@ public abstract class AbstractDelegatingSessionBuilder<T extends SessionBuilder>
}
@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;
}
}

View File

@ -12,9 +12,7 @@ package org.hibernate.engine.spi;
*
* @author Gunnar Morling
*/
public abstract class AbstractDelegatingSessionBuilderImplementor<T extends SessionBuilderImplementor>
extends AbstractDelegatingSessionBuilder<T>
implements SessionBuilderImplementor<T> {
public abstract class AbstractDelegatingSessionBuilderImplementor extends AbstractDelegatingSessionBuilder implements SessionBuilderImplementor {
public AbstractDelegatingSessionBuilderImplementor(SessionBuilderImplementor delegate) {
super( delegate );

View File

@ -24,7 +24,7 @@ import org.hibernate.resource.jdbc.spi.StatementInspector;
* @author Gunnar Morling
* @author Guillaume Smet
*/
public abstract class AbstractDelegatingSharedSessionBuilder<T extends SharedSessionBuilder> implements SharedSessionBuilder<T> {
public abstract class AbstractDelegatingSharedSessionBuilder implements SharedSessionBuilder {
private final SharedSessionBuilder delegate;
@ -33,8 +33,8 @@ public abstract class AbstractDelegatingSharedSessionBuilder<T extends SharedSes
}
@SuppressWarnings("unchecked")
protected T getThis() {
return (T) this;
protected SharedSessionBuilder getThis() {
return this;
}
public SharedSessionBuilder delegate() {
@ -47,122 +47,122 @@ public abstract class AbstractDelegatingSharedSessionBuilder<T extends SharedSes
}
@Override
public T interceptor() {
public SharedSessionBuilder interceptor() {
delegate.interceptor();
return getThis();
return this;
}
@Override
public T connection() {
public SharedSessionBuilder connection() {
delegate.connection();
return getThis();
return this;
}
@Override @Deprecated(since = "6.0")
public T connectionReleaseMode() {
public SharedSessionBuilder connectionReleaseMode() {
delegate.connectionReleaseMode();
return getThis();
return this;
}
@Override
public T connectionHandlingMode() {
public SharedSessionBuilder connectionHandlingMode() {
delegate.connectionHandlingMode();
return getThis();
return this;
}
@Override
public T autoJoinTransactions() {
public SharedSessionBuilder autoJoinTransactions() {
delegate.autoJoinTransactions();
return getThis();
return this;
}
@Override
public T autoClose() {
public SharedSessionBuilder autoClose() {
delegate.autoClose();
return getThis();
return this;
}
@Override
public T interceptor(Interceptor interceptor) {
public SharedSessionBuilder interceptor(Interceptor interceptor) {
delegate.interceptor( interceptor );
return getThis();
return this;
}
@Override
public T noInterceptor() {
public SharedSessionBuilder noInterceptor() {
delegate.noInterceptor();
return getThis();
return this;
}
@Override
public T statementInspector(StatementInspector statementInspector) {
public SharedSessionBuilder statementInspector(StatementInspector statementInspector) {
delegate.statementInspector( statementInspector );
return getThis();
return this;
}
@Override
public T connection(Connection connection) {
public SharedSessionBuilder connection(Connection connection) {
delegate.connection( connection );
return getThis();
return this;
}
@Override
public T autoJoinTransactions(boolean autoJoinTransactions) {
public SharedSessionBuilder autoJoinTransactions(boolean autoJoinTransactions) {
delegate.autoJoinTransactions( autoJoinTransactions );
return getThis();
return this;
}
@Override
public T autoClose(boolean autoClose) {
public SharedSessionBuilder autoClose(boolean autoClose) {
delegate.autoClose( autoClose );
return getThis();
return this;
}
@Override
public T tenantIdentifier(String tenantIdentifier) {
public SharedSessionBuilder tenantIdentifier(String tenantIdentifier) {
delegate.tenantIdentifier( tenantIdentifier );
return getThis();
return this;
}
@Override
public T eventListeners(SessionEventListener... listeners) {
public SharedSessionBuilder eventListeners(SessionEventListener... listeners) {
delegate.eventListeners( listeners );
return getThis();
return this;
}
@Override
public T clearEventListeners() {
public SharedSessionBuilder clearEventListeners() {
delegate.clearEventListeners();
return getThis();
return this;
}
@Override
public T connectionHandlingMode(PhysicalConnectionHandlingMode mode) {
public SharedSessionBuilder connectionHandlingMode(PhysicalConnectionHandlingMode mode) {
delegate.connectionHandlingMode( mode );
return getThis();
return this;
}
@Override
public T autoClear(boolean autoClear) {
public SharedSessionBuilder autoClear(boolean autoClear) {
delegate.autoClear( autoClear );
return getThis();
return this;
}
@Override
public T flushMode(FlushMode flushMode) {
public SharedSessionBuilder flushMode(FlushMode flushMode) {
delegate.flushMode( flushMode );
return getThis();
return this;
}
@Override
public T flushMode() {
public SharedSessionBuilder flushMode() {
delegate.flushMode();
return getThis();
return this;
}
@Override
public T jdbcTimeZone(TimeZone timeZone) {
public SharedSessionBuilder jdbcTimeZone(TimeZone timeZone) {
delegate.jdbcTimeZone( timeZone );
return getThis();
return this;
}
}

View File

@ -16,5 +16,5 @@ import org.hibernate.SessionBuilder;
*
* @author Gail Badner
*/
public interface SessionBuilderImplementor<T extends SessionBuilder> extends SessionBuilder<T> {
public interface SessionBuilderImplementor extends SessionBuilder {
}

View File

@ -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();
}

View File

@ -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();

View File

@ -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<T extends SessionBuilder> implements SessionBuilderImplementor<T>, 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;
}
}

View File

@ -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<T extends SharedSessionBuilder>
extends SessionFactoryImpl.SessionBuilderImpl<T>
implements SharedSessionBuilder<T>, 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;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -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 );

View File

@ -385,7 +385,7 @@ public abstract class AbstractSqlAstTranslator<T extends JdbcOperation> implemen
@Override
protected SessionImplementor delegate() {
if ( session == null ) {
session = (SessionImplementor) sessionFactory.openTemporarySession();
session = sessionFactory.openTemporarySession();
}
return session;
}

View File

@ -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;

View File

@ -19,7 +19,7 @@ import org.hibernate.engine.spi.AbstractDelegatingSessionBuilder;
* @author Guillaume Smet
*/
@SuppressWarnings("unused")
public class TestDelegatingSessionBuilder extends AbstractDelegatingSessionBuilder<TestDelegatingSessionBuilder> {
public class TestDelegatingSessionBuilder extends AbstractDelegatingSessionBuilder {
@SuppressWarnings("rawtypes")
public TestDelegatingSessionBuilder(SessionBuilder delegate) {

View File

@ -19,9 +19,9 @@ import org.hibernate.engine.spi.SessionBuilderImplementor;
* @author Guillaume Smet
*/
@SuppressWarnings("unused")
public class TestDelegatingSessionBuilderImplementor extends AbstractDelegatingSessionBuilderImplementor<TestDelegatingSessionBuilderImplementor> {
public class TestDelegatingSessionBuilderImplementor extends AbstractDelegatingSessionBuilderImplementor {
public TestDelegatingSessionBuilderImplementor(SessionBuilderImplementor<TestDelegatingSessionBuilderImplementor> delegate) {
public TestDelegatingSessionBuilderImplementor(SessionBuilderImplementor delegate) {
super( delegate );
}

View File

@ -20,7 +20,7 @@ import org.hibernate.engine.spi.AbstractDelegatingSharedSessionBuilder;
* @author Guillaume Smet
*/
@SuppressWarnings("unused")
public class TestDelegatingSharedSessionBuilder extends AbstractDelegatingSharedSessionBuilder<TestDelegatingSharedSessionBuilder> {
public class TestDelegatingSharedSessionBuilder extends AbstractDelegatingSharedSessionBuilder {
@SuppressWarnings("rawtypes")
public TestDelegatingSharedSessionBuilder(SharedSessionBuilder delegate) {

View File

@ -321,7 +321,7 @@ public class SessionFactoryExtension
public void inSession(Consumer<SessionImplementor> 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> T fromSession(Function<SessionImplementor, T> 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<SessionImplementor> 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> T fromTransaction(Function<SessionImplementor, T> 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 );
}