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 committed by Andrea Boriero
parent 2ee1c970a3
commit aac3f5fc85
3 changed files with 21 additions and 10 deletions

View File

@ -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<R> extends AbstractScrollableResults<R> {
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();
}

View File

@ -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,

View File

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