diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/AssemblerCreationState.java b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/AssemblerCreationState.java index 28d1a6dc51..071faee857 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/AssemblerCreationState.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/AssemblerCreationState.java @@ -18,11 +18,14 @@ import org.hibernate.sql.exec.spi.ExecutionContext; * @author Steve Ebersole */ public interface AssemblerCreationState { - default boolean isScrollResult() { return false; } + default boolean isDynamicInstantiation() { + return false; + } + LockMode determineEffectiveLockMode(String identificationVariable); Initializer resolveInitializer( diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntitySelectFetchInitializerBuilder.java b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntitySelectFetchInitializerBuilder.java index 48f47701b9..fee1cbfc0a 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntitySelectFetchInitializerBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntitySelectFetchInitializerBuilder.java @@ -94,6 +94,9 @@ public class EntitySelectFetchInitializerBuilder { .effectivelyBatchLoadable( entityPersister ) ) { return NONE; } + else if ( creationState.isDynamicInstantiation() ) { + return BatchMode.BATCH_INITIALIZE; + } while ( parentAccess.isEmbeddableInitializer() ) { final EmbeddableInitializer embeddableInitializer = parentAccess.asEmbeddableInitializer(); final EmbeddableValuedModelPart initializedPart = embeddableInitializer.getInitializedPart(); diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/internal/ResultsHelper.java b/hibernate-core/src/main/java/org/hibernate/sql/results/internal/ResultsHelper.java index 29e3669b95..58aa4049a0 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/internal/ResultsHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/internal/ResultsHelper.java @@ -6,14 +6,12 @@ */ package org.hibernate.sql.results.internal; -import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.function.Supplier; import org.hibernate.CacheMode; -import org.hibernate.HibernateException; import org.hibernate.LockMode; import org.hibernate.LockOptions; import org.hibernate.cache.spi.access.CollectionDataAccess; @@ -28,7 +26,6 @@ import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.internal.CoreLogging; import org.hibernate.internal.CoreMessageLogger; -import org.hibernate.metamodel.mapping.JdbcMapping; import org.hibernate.metamodel.mapping.ModelPart; import org.hibernate.metamodel.mapping.PluralAttributeMapping; import org.hibernate.persister.collection.CollectionPersister; @@ -42,6 +39,7 @@ import org.hibernate.sql.results.ResultsLogger; import org.hibernate.sql.results.graph.AssemblerCreationState; import org.hibernate.sql.results.graph.DomainResultAssembler; import org.hibernate.sql.results.graph.Initializer; +import org.hibernate.sql.results.graph.instantiation.DynamicInstantiationResult; import org.hibernate.sql.results.jdbc.spi.JdbcValues; import org.hibernate.sql.results.jdbc.spi.JdbcValuesMapping; import org.hibernate.sql.results.spi.RowReader; @@ -77,12 +75,23 @@ public class ResultsHelper { final List> assemblers = jdbcValuesMapping.resolveAssemblers( new AssemblerCreationState() { + Boolean dynamicInstantiation; @Override public boolean isScrollResult() { return executionContext.isScrollResult(); } + @Override + public boolean isDynamicInstantiation() { + if ( dynamicInstantiation == null ) { + dynamicInstantiation = jdbcValuesMapping.getDomainResults() + .stream() + .anyMatch( domainResult -> domainResult instanceof DynamicInstantiationResult ); + } + return dynamicInstantiation; + } + @Override public LockMode determineEffectiveLockMode(String identificationVariable) { return lockOptions.getEffectiveLockMode( identificationVariable );