From 052aad0ed41b9c1e9e3b11d8e16b755ca11b2416 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Mon, 30 Nov 2015 11:13:58 -0600 Subject: [PATCH] HHH-10313 - Make SessionImplementor extend WrapperOptions --- .../engine/spi/SessionDelegatorBaseImpl.java | 18 ++++++- .../internal/AbstractSessionImpl.java | 14 +++++- .../internal/WrapperOptionsImpl.java | 48 +++++++++++++++++++ .../type/AbstractStandardBasicType.java | 28 +++-------- .../descriptor/WrapperOptionsContext.java | 21 ++++++++ 5 files changed, 105 insertions(+), 24 deletions(-) create mode 100644 hibernate-core/src/main/java/org/hibernate/internal/WrapperOptionsImpl.java create mode 100644 hibernate-core/src/main/java/org/hibernate/type/descriptor/WrapperOptionsContext.java diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionDelegatorBaseImpl.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionDelegatorBaseImpl.java index 5bbe3c4758..b1c062baab 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionDelegatorBaseImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionDelegatorBaseImpl.java @@ -39,6 +39,7 @@ import org.hibernate.collection.spi.PersistentCollection; import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess; import org.hibernate.engine.jdbc.spi.JdbcCoordinator; import org.hibernate.engine.query.spi.sql.NativeSQLQuerySpecification; +import org.hibernate.internal.WrapperOptionsImpl; import org.hibernate.jdbc.ReturningWork; import org.hibernate.jdbc.Work; import org.hibernate.loader.custom.CustomQuery; @@ -46,6 +47,8 @@ import org.hibernate.persister.entity.EntityPersister; import org.hibernate.procedure.ProcedureCall; import org.hibernate.resource.transaction.TransactionCoordinator; import org.hibernate.stat.SessionStatistics; +import org.hibernate.type.descriptor.WrapperOptions; +import org.hibernate.type.descriptor.WrapperOptionsContext; /** * This class is meant to be extended. @@ -57,7 +60,7 @@ import org.hibernate.stat.SessionStatistics; * * @author Sanne Grinovero (C) 2012 Red Hat Inc. */ -public class SessionDelegatorBaseImpl implements SessionImplementor, Session { +public class SessionDelegatorBaseImpl implements SessionImplementor, Session, WrapperOptionsContext { protected final SessionImplementor sessionImplementor; protected final Session session; @@ -766,4 +769,17 @@ public class SessionDelegatorBaseImpl implements SessionImplementor, Session { public void addEventListeners(SessionEventListener... listeners) { session.addEventListeners( listeners ); } + + @Override + public WrapperOptions getWrapperOptions() { + if ( sessionImplementor instanceof WrapperOptionsContext ) { + return ( (WrapperOptionsContext) sessionImplementor ).getWrapperOptions(); + } + else if ( session instanceof WrapperOptionsContext ) { + return ( (WrapperOptionsContext) session ).getWrapperOptions(); + } + else { + return new WrapperOptionsImpl( sessionImplementor ); + } + } } diff --git a/hibernate-core/src/main/java/org/hibernate/internal/AbstractSessionImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/AbstractSessionImpl.java index 1129c39ddb..48d7fe6573 100755 --- a/hibernate-core/src/main/java/org/hibernate/internal/AbstractSessionImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/AbstractSessionImpl.java @@ -57,6 +57,8 @@ import org.hibernate.resource.transaction.TransactionCoordinatorBuilder; import org.hibernate.resource.transaction.TransactionCoordinatorBuilder.TransactionCoordinatorOptions; import org.hibernate.resource.transaction.spi.TransactionStatus; import org.hibernate.service.ServiceRegistry; +import org.hibernate.type.descriptor.WrapperOptions; +import org.hibernate.type.descriptor.WrapperOptionsContext; /** * Functionality common to stateless and stateful sessions @@ -64,12 +66,14 @@ import org.hibernate.service.ServiceRegistry; * @author Gavin King */ public abstract class AbstractSessionImpl - implements Serializable, SharedSessionContract, SessionImplementor, JdbcSessionOwner, TransactionCoordinatorOptions { + implements Serializable, SharedSessionContract, SessionImplementor, JdbcSessionOwner, TransactionCoordinatorOptions, WrapperOptionsContext { + protected transient SessionFactoryImpl factory; private final String tenantIdentifier; private boolean closed; protected transient Transaction currentHibernateTransaction; + protected transient WrapperOptionsImpl wrapperOptions; protected AbstractSessionImpl(SessionFactoryImpl factory, String tenantIdentifier) { this.factory = factory; @@ -589,4 +593,12 @@ public abstract class AbstractSessionImpl return factory.getServiceRegistry().getService( TransactionCoordinatorBuilder.class ); } + @Override + public WrapperOptions getWrapperOptions() { + if ( wrapperOptions == null ) { + wrapperOptions = new WrapperOptionsImpl( this ); + } + + return wrapperOptions; + } } diff --git a/hibernate-core/src/main/java/org/hibernate/internal/WrapperOptionsImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/WrapperOptionsImpl.java new file mode 100644 index 0000000000..f4ad1abd04 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/internal/WrapperOptionsImpl.java @@ -0,0 +1,48 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.internal; + +import org.hibernate.Hibernate; +import org.hibernate.cfg.Environment; +import org.hibernate.engine.jdbc.LobCreator; +import org.hibernate.engine.spi.SessionImplementor; +import org.hibernate.type.descriptor.WrapperOptions; +import org.hibernate.type.descriptor.sql.SqlTypeDescriptor; + +/** + * @author Steve Ebersole + */ +public class WrapperOptionsImpl implements WrapperOptions { + private final SessionImplementor session; + + private final boolean useStreamForLobBinding; + + public WrapperOptionsImpl(SessionImplementor session) { + this.session = session; + + this.useStreamForLobBinding = Environment.useStreamsForBinary() + || session.getFactory().getDialect().useInputStreamToInsertBlob(); + } + + @Override + public boolean useStreamForLobBinding() { + return useStreamForLobBinding; + } + + @Override + public LobCreator getLobCreator() { + return Hibernate.getLobCreator( session ); + } + + @Override + public SqlTypeDescriptor remapSqlTypeDescriptor(SqlTypeDescriptor sqlTypeDescriptor) { + final SqlTypeDescriptor remapped = sqlTypeDescriptor.canBeRemapped() + ? session.getFactory().getDialect().remapSqlTypeDescriptor( sqlTypeDescriptor ) + : sqlTypeDescriptor; + return remapped == null ? sqlTypeDescriptor : remapped; + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/type/AbstractStandardBasicType.java b/hibernate-core/src/main/java/org/hibernate/type/AbstractStandardBasicType.java index 41d1cc45b9..6404aedee9 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/AbstractStandardBasicType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/AbstractStandardBasicType.java @@ -13,23 +13,20 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.Map; -import org.hibernate.Hibernate; import org.hibernate.HibernateException; import org.hibernate.MappingException; -import org.hibernate.cfg.Environment; -import org.hibernate.engine.jdbc.LobCreator; import org.hibernate.engine.jdbc.Size; import org.hibernate.engine.spi.Mapping; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionImplementor; +import org.hibernate.internal.WrapperOptionsImpl; import org.hibernate.internal.util.collections.ArrayHelper; import org.hibernate.type.descriptor.WrapperOptions; +import org.hibernate.type.descriptor.WrapperOptionsContext; import org.hibernate.type.descriptor.java.JavaTypeDescriptor; import org.hibernate.type.descriptor.java.MutabilityPlan; import org.hibernate.type.descriptor.sql.SqlTypeDescriptor; -import org.dom4j.Node; - /** * Convenience base class for {@link BasicType} implementations * @@ -354,24 +351,11 @@ public abstract class AbstractStandardBasicType return remapSqlTypeDescriptor( options ).getExtractor( javaTypeDescriptor ).extract( statement, paramNames, options ); } - // TODO : have SessionImplementor extend WrapperOptions private WrapperOptions getOptions(final SessionImplementor session) { - return new WrapperOptions() { - public boolean useStreamForLobBinding() { - return Environment.useStreamsForBinary() - || session.getFactory().getDialect().useInputStreamToInsertBlob(); - } + if ( session instanceof WrapperOptionsContext ) { + return ( (WrapperOptionsContext) session ).getWrapperOptions(); + } - public LobCreator getLobCreator() { - return Hibernate.getLobCreator( session ); - } - - public SqlTypeDescriptor remapSqlTypeDescriptor(SqlTypeDescriptor sqlTypeDescriptor) { - final SqlTypeDescriptor remapped = sqlTypeDescriptor.canBeRemapped() - ? session.getFactory().getDialect().remapSqlTypeDescriptor( sqlTypeDescriptor ) - : sqlTypeDescriptor; - return remapped == null ? sqlTypeDescriptor : remapped; - } - }; + return new WrapperOptionsImpl( session ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/WrapperOptionsContext.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/WrapperOptionsContext.java new file mode 100644 index 0000000000..9c30d8ef3e --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/WrapperOptionsContext.java @@ -0,0 +1,21 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.type.descriptor; + +/** + * Defines the context for {@link WrapperOptions} + * + * @author Steve Ebersole + */ +public interface WrapperOptionsContext { + /** + * Obtain the WrapperOptions for this context. + * + * @return The WrapperOptions + */ + WrapperOptions getWrapperOptions(); +}