HHH-16184 Two queries are execute to initialize empty collections

This commit is contained in:
Andrea Boriero 2023-02-14 16:35:40 +01:00
parent 66ef965320
commit fb901051ca
3 changed files with 21 additions and 10 deletions

View File

@ -7,6 +7,7 @@
package org.hibernate.internal; package org.hibernate.internal;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.engine.spi.PersistenceContext;
import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.sql.results.internal.RowProcessingStateStandardImpl; import org.hibernate.sql.results.internal.RowProcessingStateStandardImpl;
import org.hibernate.sql.results.jdbc.internal.JdbcValuesSourceProcessingStateStandardImpl; import org.hibernate.sql.results.jdbc.internal.JdbcValuesSourceProcessingStateStandardImpl;
@ -122,15 +123,22 @@ public class ScrollableResultsImpl<R> extends AbstractScrollableResults<R> {
return; return;
} }
currentRow = getRowReader().readRow( final PersistenceContext persistenceContext = getPersistenceContext().getPersistenceContext();
getRowProcessingState(),
getProcessingOptions()
);
getRowProcessingState().finishRowProcessing(); persistenceContext.beforeLoad();
getJdbcValuesSourceProcessingState().finishUp(); try {
currentRow = getRowReader().readRow(
getRowProcessingState(),
getProcessingOptions()
);
getRowProcessingState().getSession().getPersistenceContext().initializeNonLazyCollections(); getRowProcessingState().finishRowProcessing();
getJdbcValuesSourceProcessingState().finishUp();
}
finally {
persistenceContext.afterLoad();
}
persistenceContext.initializeNonLazyCollections();
afterScrollOperation(); afterScrollOperation();
} }

View File

@ -291,9 +291,11 @@ public class JdbcSelectExecutorStandardImpl implements JdbcSelectExecutor {
} }
} }
final SharedSessionContractImplementor session = executionContext.getSession();
final boolean stats; final boolean stats;
long startTime = 0; long startTime = 0;
final StatisticsImplementor statistics = executionContext.getSession().getFactory().getStatistics(); final StatisticsImplementor statistics = session.getFactory().getStatistics();
if ( executionContext.hasQueryExecutionToBeAddedToStatistics() if ( executionContext.hasQueryExecutionToBeAddedToStatistics()
&& jdbcValues instanceof JdbcValuesResultSetImpl ) { && jdbcValues instanceof JdbcValuesResultSetImpl ) {
stats = statistics.isStatisticsEnabled(); stats = statistics.isStatisticsEnabled();
@ -359,7 +361,7 @@ public class JdbcSelectExecutorStandardImpl implements JdbcSelectExecutor {
final T result = resultsConsumer.consume( final T result = resultsConsumer.consume(
jdbcValues, jdbcValues,
executionContext.getSession(), session,
processingOptions, processingOptions,
valuesProcessingState, valuesProcessingState,
rowProcessingState, rowProcessingState,

View File

@ -153,6 +153,7 @@ public class ListResultsConsumer<R> implements ResultsConsumer<List<R>, R> {
final QueryOptions queryOptions = rowProcessingState.getQueryOptions(); final QueryOptions queryOptions = rowProcessingState.getQueryOptions();
RuntimeException ex = null; RuntimeException ex = null;
try { try {
persistenceContext.beforeLoad();
persistenceContext.getLoadContexts().register( jdbcValuesSourceProcessingState ); persistenceContext.getLoadContexts().register( jdbcValuesSourceProcessingState );
final JavaType<R> domainResultJavaType = resolveDomainResultJavaType( final JavaType<R> domainResultJavaType = resolveDomainResultJavaType(
@ -221,8 +222,8 @@ public class ListResultsConsumer<R> implements ResultsConsumer<List<R>, R> {
} }
finally { finally {
try { try {
jdbcValues.finishUp( session ); jdbcValues.finishUp( session );
persistenceContext.afterLoad();
persistenceContext.initializeNonLazyCollections(); persistenceContext.initializeNonLazyCollections();
} }
catch (RuntimeException e) { catch (RuntimeException e) {