HHH-10313 - Make SessionImplementor extend WrapperOptions

This commit is contained in:
Steve Ebersole 2015-11-30 11:13:58 -06:00
parent 59a5a88dbd
commit 052aad0ed4
5 changed files with 105 additions and 24 deletions

View File

@ -39,6 +39,7 @@ import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess; import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess;
import org.hibernate.engine.jdbc.spi.JdbcCoordinator; import org.hibernate.engine.jdbc.spi.JdbcCoordinator;
import org.hibernate.engine.query.spi.sql.NativeSQLQuerySpecification; import org.hibernate.engine.query.spi.sql.NativeSQLQuerySpecification;
import org.hibernate.internal.WrapperOptionsImpl;
import org.hibernate.jdbc.ReturningWork; import org.hibernate.jdbc.ReturningWork;
import org.hibernate.jdbc.Work; import org.hibernate.jdbc.Work;
import org.hibernate.loader.custom.CustomQuery; import org.hibernate.loader.custom.CustomQuery;
@ -46,6 +47,8 @@ import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.procedure.ProcedureCall; import org.hibernate.procedure.ProcedureCall;
import org.hibernate.resource.transaction.TransactionCoordinator; import org.hibernate.resource.transaction.TransactionCoordinator;
import org.hibernate.stat.SessionStatistics; import org.hibernate.stat.SessionStatistics;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.WrapperOptionsContext;
/** /**
* This class is meant to be extended. * This class is meant to be extended.
@ -57,7 +60,7 @@ import org.hibernate.stat.SessionStatistics;
* *
* @author Sanne Grinovero <sanne@hibernate.org> (C) 2012 Red Hat Inc. * @author Sanne Grinovero <sanne@hibernate.org> (C) 2012 Red Hat Inc.
*/ */
public class SessionDelegatorBaseImpl implements SessionImplementor, Session { public class SessionDelegatorBaseImpl implements SessionImplementor, Session, WrapperOptionsContext {
protected final SessionImplementor sessionImplementor; protected final SessionImplementor sessionImplementor;
protected final Session session; protected final Session session;
@ -766,4 +769,17 @@ public class SessionDelegatorBaseImpl implements SessionImplementor, Session {
public void addEventListeners(SessionEventListener... listeners) { public void addEventListeners(SessionEventListener... listeners) {
session.addEventListeners( 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 );
}
}
} }

View File

@ -57,6 +57,8 @@ import org.hibernate.resource.transaction.TransactionCoordinatorBuilder;
import org.hibernate.resource.transaction.TransactionCoordinatorBuilder.TransactionCoordinatorOptions; import org.hibernate.resource.transaction.TransactionCoordinatorBuilder.TransactionCoordinatorOptions;
import org.hibernate.resource.transaction.spi.TransactionStatus; import org.hibernate.resource.transaction.spi.TransactionStatus;
import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistry;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.WrapperOptionsContext;
/** /**
* Functionality common to stateless and stateful sessions * Functionality common to stateless and stateful sessions
@ -64,12 +66,14 @@ import org.hibernate.service.ServiceRegistry;
* @author Gavin King * @author Gavin King
*/ */
public abstract class AbstractSessionImpl public abstract class AbstractSessionImpl
implements Serializable, SharedSessionContract, SessionImplementor, JdbcSessionOwner, TransactionCoordinatorOptions { implements Serializable, SharedSessionContract, SessionImplementor, JdbcSessionOwner, TransactionCoordinatorOptions, WrapperOptionsContext {
protected transient SessionFactoryImpl factory; protected transient SessionFactoryImpl factory;
private final String tenantIdentifier; private final String tenantIdentifier;
private boolean closed; private boolean closed;
protected transient Transaction currentHibernateTransaction; protected transient Transaction currentHibernateTransaction;
protected transient WrapperOptionsImpl wrapperOptions;
protected AbstractSessionImpl(SessionFactoryImpl factory, String tenantIdentifier) { protected AbstractSessionImpl(SessionFactoryImpl factory, String tenantIdentifier) {
this.factory = factory; this.factory = factory;
@ -589,4 +593,12 @@ public abstract class AbstractSessionImpl
return factory.getServiceRegistry().getService( TransactionCoordinatorBuilder.class ); return factory.getServiceRegistry().getService( TransactionCoordinatorBuilder.class );
} }
@Override
public WrapperOptions getWrapperOptions() {
if ( wrapperOptions == null ) {
wrapperOptions = new WrapperOptionsImpl( this );
}
return wrapperOptions;
}
} }

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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;
}
}

View File

@ -13,23 +13,20 @@ import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Map; import java.util.Map;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.MappingException; 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.jdbc.Size;
import org.hibernate.engine.spi.Mapping; import org.hibernate.engine.spi.Mapping;
import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.internal.WrapperOptionsImpl;
import org.hibernate.internal.util.collections.ArrayHelper; import org.hibernate.internal.util.collections.ArrayHelper;
import org.hibernate.type.descriptor.WrapperOptions; 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.JavaTypeDescriptor;
import org.hibernate.type.descriptor.java.MutabilityPlan; import org.hibernate.type.descriptor.java.MutabilityPlan;
import org.hibernate.type.descriptor.sql.SqlTypeDescriptor; import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;
import org.dom4j.Node;
/** /**
* Convenience base class for {@link BasicType} implementations * Convenience base class for {@link BasicType} implementations
* *
@ -354,24 +351,11 @@ public abstract class AbstractStandardBasicType<T>
return remapSqlTypeDescriptor( options ).getExtractor( javaTypeDescriptor ).extract( statement, paramNames, options ); return remapSqlTypeDescriptor( options ).getExtractor( javaTypeDescriptor ).extract( statement, paramNames, options );
} }
// TODO : have SessionImplementor extend WrapperOptions
private WrapperOptions getOptions(final SessionImplementor session) { private WrapperOptions getOptions(final SessionImplementor session) {
return new WrapperOptions() { if ( session instanceof WrapperOptionsContext ) {
public boolean useStreamForLobBinding() { return ( (WrapperOptionsContext) session ).getWrapperOptions();
return Environment.useStreamsForBinary() }
|| session.getFactory().getDialect().useInputStreamToInsertBlob();
}
public LobCreator getLobCreator() { return new WrapperOptionsImpl( session );
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;
}
};
} }
} }

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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();
}