HHH-16981 Improve JdbcOperation caching

This commit is contained in:
Christian Beikov 2023-07-24 12:48:45 +02:00
parent 023f6763d1
commit 7915ad635a
2 changed files with 12 additions and 13 deletions

View File

@ -41,6 +41,7 @@ public class CollectionLoaderSingleKey implements CollectionLoader {
private final int keyJdbcCount; private final int keyJdbcCount;
private final SelectStatement sqlAst; private final SelectStatement sqlAst;
private final JdbcOperationQuerySelect jdbcSelect;
private final JdbcParametersList jdbcParameters; private final JdbcParametersList jdbcParameters;
public CollectionLoaderSingleKey( public CollectionLoaderSingleKey(
@ -64,6 +65,11 @@ public class CollectionLoaderSingleKey implements CollectionLoader {
sessionFactory sessionFactory
); );
this.jdbcParameters = jdbcParametersBuilder.build(); this.jdbcParameters = jdbcParametersBuilder.build();
this.jdbcSelect = sessionFactory.getJdbcServices()
.getJdbcEnvironment()
.getSqlAstTranslatorFactory()
.buildSelectTranslator( sessionFactory, sqlAst )
.translate( JdbcParameterBindings.NO_BINDINGS, QueryOptions.NONE );
} }
@Override @Override
@ -87,10 +93,7 @@ public class CollectionLoaderSingleKey implements CollectionLoader {
public PersistentCollection<?> load(Object key, SharedSessionContractImplementor session) { public PersistentCollection<?> load(Object key, SharedSessionContractImplementor session) {
final CollectionKey collectionKey = new CollectionKey( attributeMapping.getCollectionDescriptor(), key ); final CollectionKey collectionKey = new CollectionKey( attributeMapping.getCollectionDescriptor(), key );
final SessionFactoryImplementor sessionFactory = session.getFactory(); final JdbcServices jdbcServices = session.getFactory().getJdbcServices();
final JdbcServices jdbcServices = sessionFactory.getJdbcServices();
final JdbcEnvironment jdbcEnvironment = jdbcServices.getJdbcEnvironment();
final SqlAstTranslatorFactory sqlAstTranslatorFactory = jdbcEnvironment.getSqlAstTranslatorFactory();
final JdbcParameterBindings jdbcParameterBindings = new JdbcParameterBindingsImpl( keyJdbcCount ); final JdbcParameterBindings jdbcParameterBindings = new JdbcParameterBindingsImpl( keyJdbcCount );
int offset = jdbcParameterBindings.registerParametersForEachJdbcValue( int offset = jdbcParameterBindings.registerParametersForEachJdbcValue(
@ -101,10 +104,6 @@ public class CollectionLoaderSingleKey implements CollectionLoader {
); );
assert offset == jdbcParameters.size(); assert offset == jdbcParameters.size();
final JdbcOperationQuerySelect jdbcSelect = sqlAstTranslatorFactory
.buildSelectTranslator( sessionFactory, sqlAst )
.translate( jdbcParameterBindings, QueryOptions.NONE );
final SubselectFetch.RegistrationHandler subSelectFetchableKeysHandler = SubselectFetch.createRegistrationHandler( final SubselectFetch.RegistrationHandler subSelectFetchableKeysHandler = SubselectFetch.createRegistrationHandler(
session.getPersistenceContext().getBatchFetchQueue(), session.getPersistenceContext().getBatchFetchQueue(),
sqlAst, sqlAst,

View File

@ -22,7 +22,6 @@ import org.hibernate.loader.ast.internal.NoCallbackExecutionContext;
import org.hibernate.metamodel.mapping.AttributeMapping; import org.hibernate.metamodel.mapping.AttributeMapping;
import org.hibernate.metamodel.mapping.EntityMappingType; import org.hibernate.metamodel.mapping.EntityMappingType;
import org.hibernate.query.spi.QueryOptions; import org.hibernate.query.spi.QueryOptions;
import org.hibernate.sql.ast.tree.expression.JdbcParameter;
import org.hibernate.sql.ast.tree.select.SelectStatement; import org.hibernate.sql.ast.tree.select.SelectStatement;
import org.hibernate.sql.exec.internal.JdbcParameterBindingsImpl; import org.hibernate.sql.exec.internal.JdbcParameterBindingsImpl;
import org.hibernate.sql.exec.spi.JdbcOperationQuerySelect; import org.hibernate.sql.exec.spi.JdbcOperationQuerySelect;
@ -46,6 +45,7 @@ import static org.hibernate.sql.results.spi.ListResultsConsumer.UniqueSemantic.F
@Incubating @Incubating
public class GeneratedValuesProcessor { public class GeneratedValuesProcessor {
private final SelectStatement selectStatement; private final SelectStatement selectStatement;
private final JdbcOperationQuerySelect jdbcSelect;
private final List<AttributeMapping> generatedValuesToSelect; private final List<AttributeMapping> generatedValuesToSelect;
private final JdbcParametersList jdbcParameters; private final JdbcParametersList jdbcParameters;
@ -62,6 +62,7 @@ public class GeneratedValuesProcessor {
generatedValuesToSelect = getGeneratedAttributes( entityDescriptor, timing ); generatedValuesToSelect = getGeneratedAttributes( entityDescriptor, timing );
if ( generatedValuesToSelect.isEmpty() ) { if ( generatedValuesToSelect.isEmpty() ) {
selectStatement = null; selectStatement = null;
jdbcSelect = null;
this.jdbcParameters = JdbcParametersList.empty(); this.jdbcParameters = JdbcParametersList.empty();
} }
else { else {
@ -78,6 +79,9 @@ public class GeneratedValuesProcessor {
builder::add, builder::add,
sessionFactory sessionFactory
); );
jdbcSelect = sessionFactory.getJdbcServices().getJdbcEnvironment().getSqlAstTranslatorFactory()
.buildSelectTranslator( sessionFactory, selectStatement )
.translate( JdbcParameterBindings.NO_BINDINGS, QueryOptions.NONE );
this.jdbcParameters = builder.build(); this.jdbcParameters = builder.build();
} }
} }
@ -116,10 +120,6 @@ public class GeneratedValuesProcessor {
private List<Object[]> executeSelect(Object id, SharedSessionContractImplementor session) { private List<Object[]> executeSelect(Object id, SharedSessionContractImplementor session) {
final JdbcParameterBindings jdbcParamBindings = getJdbcParameterBindings( id, session ); final JdbcParameterBindings jdbcParamBindings = getJdbcParameterBindings( id, session );
final JdbcOperationQuerySelect jdbcSelect =
sessionFactory.getJdbcServices().getJdbcEnvironment().getSqlAstTranslatorFactory()
.buildSelectTranslator( sessionFactory, selectStatement )
.translate( jdbcParamBindings, QueryOptions.NONE );
return session.getFactory().getJdbcServices().getJdbcSelectExecutor() return session.getFactory().getJdbcServices().getJdbcSelectExecutor()
.list( jdbcSelect, jdbcParamBindings, new NoCallbackExecutionContext(session), (row) -> row, FILTER ); .list( jdbcSelect, jdbcParamBindings, new NoCallbackExecutionContext(session), (row) -> row, FILTER );
} }