From fb901051caec95f50f0abf1b3e68e1da7dc2b0ca Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Tue, 14 Feb 2023 16:35:40 +0100 Subject: [PATCH] HHH-16184 Two queries are execute to initialize empty collections --- .../internal/ScrollableResultsImpl.java | 22 +++++++++++++------ .../JdbcSelectExecutorStandardImpl.java | 6 +++-- .../sql/results/spi/ListResultsConsumer.java | 3 ++- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/internal/ScrollableResultsImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/ScrollableResultsImpl.java index df2858e7b9..0c7304e6a0 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/ScrollableResultsImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/ScrollableResultsImpl.java @@ -7,6 +7,7 @@ package org.hibernate.internal; import org.hibernate.HibernateException; +import org.hibernate.engine.spi.PersistenceContext; import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.sql.results.internal.RowProcessingStateStandardImpl; import org.hibernate.sql.results.jdbc.internal.JdbcValuesSourceProcessingStateStandardImpl; @@ -122,15 +123,22 @@ public class ScrollableResultsImpl extends AbstractScrollableResults { return; } - currentRow = getRowReader().readRow( - getRowProcessingState(), - getProcessingOptions() - ); + final PersistenceContext persistenceContext = getPersistenceContext().getPersistenceContext(); - getRowProcessingState().finishRowProcessing(); - getJdbcValuesSourceProcessingState().finishUp(); + persistenceContext.beforeLoad(); + try { + currentRow = getRowReader().readRow( + getRowProcessingState(), + getProcessingOptions() + ); - getRowProcessingState().getSession().getPersistenceContext().initializeNonLazyCollections(); + getRowProcessingState().finishRowProcessing(); + getJdbcValuesSourceProcessingState().finishUp(); + } + finally { + persistenceContext.afterLoad(); + } + persistenceContext.initializeNonLazyCollections(); afterScrollOperation(); } diff --git a/hibernate-core/src/main/java/org/hibernate/sql/exec/internal/JdbcSelectExecutorStandardImpl.java b/hibernate-core/src/main/java/org/hibernate/sql/exec/internal/JdbcSelectExecutorStandardImpl.java index 6740f0ce87..dbd3012591 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/exec/internal/JdbcSelectExecutorStandardImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/exec/internal/JdbcSelectExecutorStandardImpl.java @@ -291,9 +291,11 @@ public class JdbcSelectExecutorStandardImpl implements JdbcSelectExecutor { } } + final SharedSessionContractImplementor session = executionContext.getSession(); + final boolean stats; long startTime = 0; - final StatisticsImplementor statistics = executionContext.getSession().getFactory().getStatistics(); + final StatisticsImplementor statistics = session.getFactory().getStatistics(); if ( executionContext.hasQueryExecutionToBeAddedToStatistics() && jdbcValues instanceof JdbcValuesResultSetImpl ) { stats = statistics.isStatisticsEnabled(); @@ -359,7 +361,7 @@ public class JdbcSelectExecutorStandardImpl implements JdbcSelectExecutor { final T result = resultsConsumer.consume( jdbcValues, - executionContext.getSession(), + session, processingOptions, valuesProcessingState, rowProcessingState, diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/spi/ListResultsConsumer.java b/hibernate-core/src/main/java/org/hibernate/sql/results/spi/ListResultsConsumer.java index 9899893230..cf421eb297 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/spi/ListResultsConsumer.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/spi/ListResultsConsumer.java @@ -153,6 +153,7 @@ public class ListResultsConsumer implements ResultsConsumer, R> { final QueryOptions queryOptions = rowProcessingState.getQueryOptions(); RuntimeException ex = null; try { + persistenceContext.beforeLoad(); persistenceContext.getLoadContexts().register( jdbcValuesSourceProcessingState ); final JavaType domainResultJavaType = resolveDomainResultJavaType( @@ -221,8 +222,8 @@ public class ListResultsConsumer implements ResultsConsumer, R> { } finally { try { - jdbcValues.finishUp( session ); + persistenceContext.afterLoad(); persistenceContext.initializeNonLazyCollections(); } catch (RuntimeException e) {