diff --git a/hibernate-core/src/main/java/org/hibernate/internal/FetchingScrollableResultsImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/FetchingScrollableResultsImpl.java index c07d689b31..7462c297c5 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/FetchingScrollableResultsImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/FetchingScrollableResultsImpl.java @@ -53,10 +53,7 @@ public class FetchingScrollableResultsImpl extends AbstractScrollableResults< private static EntityInitializer extractResultInitializer(RowReader rowReader) { Initializer initializer = rowReader.getInitializers().get( rowReader.getInitializers().size() - 1 ); - if ( initializer.isEntityInitializer() ) { - return (EntityInitializer) initializer; - } - return null; + return initializer.asEntityInitializer(); //might return null when it's not an EntityInitializer (intentional) } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/query/results/complete/EntityResultImpl.java b/hibernate-core/src/main/java/org/hibernate/query/results/complete/EntityResultImpl.java index ff87e338d5..6be6423eed 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/results/complete/EntityResultImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/query/results/complete/EntityResultImpl.java @@ -25,6 +25,7 @@ import org.hibernate.sql.results.graph.DomainResultCreationState; import org.hibernate.sql.results.graph.Fetch; import org.hibernate.sql.results.graph.FetchParentAccess; import org.hibernate.sql.results.graph.Fetchable; +import org.hibernate.sql.results.graph.Initializer; import org.hibernate.sql.results.graph.basic.BasicFetch; import org.hibernate.sql.results.graph.entity.EntityInitializer; import org.hibernate.sql.results.graph.entity.EntityResult; @@ -160,7 +161,7 @@ public class EntityResultImpl implements EntityResult { public DomainResultAssembler createResultAssembler( FetchParentAccess parentAccess, AssemblerCreationState creationState) { - final EntityInitializer initializer = (EntityInitializer) creationState.resolveInitializer( + final Initializer initializer = creationState.resolveInitializer( getNavigablePath(), getReferencedModePart(), () -> new EntityResultInitializer( @@ -174,6 +175,6 @@ public class EntityResultImpl implements EntityResult { ) ); - return new EntityAssembler( getResultJavaType(), initializer ); + return new EntityAssembler( getResultJavaType(), initializer.asEntityInitializer() ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/FetchParentAccess.java b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/FetchParentAccess.java index 9916060e4f..b8d39ec1d4 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/FetchParentAccess.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/FetchParentAccess.java @@ -23,11 +23,12 @@ public interface FetchParentAccess extends Initializer { */ FetchParentAccess findFirstEntityDescriptorAccess(); - default EntityInitializer findFirstEntityInitializer(){ - if ( this.isEntityInitializer() ) { - return (EntityInitializer) this; + default EntityInitializer findFirstEntityInitializer() { + final EntityInitializer entityInitializer = this.asEntityInitializer(); + if ( entityInitializer != null ) { + return entityInitializer; } - return (EntityInitializer) findFirstEntityDescriptorAccess(); + return findFirstEntityDescriptorAccess().asEntityInitializer(); } Object getParentKey(); diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/Initializer.java b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/Initializer.java index 795b26c270..287a36358a 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/Initializer.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/Initializer.java @@ -9,6 +9,7 @@ package org.hibernate.sql.results.graph; import org.hibernate.Incubating; import org.hibernate.metamodel.mapping.AttributeMapping; import org.hibernate.persister.entity.EntityPersister; +import org.hibernate.sql.results.graph.entity.EntityInitializer; import org.hibernate.sql.results.jdbc.spi.RowProcessingState; import org.hibernate.metamodel.mapping.ModelPart; import org.hibernate.spi.NavigablePath; @@ -103,4 +104,8 @@ public interface Initializer { return false; } + default EntityInitializer asEntityInitializer() { + return null; + } + } diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/embeddable/AbstractEmbeddableInitializer.java b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/embeddable/AbstractEmbeddableInitializer.java index da84ca15c0..70208b356a 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/embeddable/AbstractEmbeddableInitializer.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/embeddable/AbstractEmbeddableInitializer.java @@ -427,8 +427,9 @@ public abstract class AbstractEmbeddableInitializer extends AbstractFetchParentA return ( (CollectionInitializer) parentInitializer ).getCollectionInstance().getOwner(); } - if ( parentInitializer.isEntityInitializer() ) { - return ( (EntityInitializer) parentInitializer ).getEntityInstance(); + final EntityInitializer parentEntityInitializer = parentInitializer.asEntityInitializer(); + if ( parentEntityInitializer != null ) { + return parentEntityInitializer.getEntityInstance(); } throw new NotYetImplementedFor6Exception( getClass() ); diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/EntityInitializer.java b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/EntityInitializer.java index 7585dd2a41..ac78d6f33c 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/EntityInitializer.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/EntityInitializer.java @@ -54,4 +54,10 @@ public interface EntityInitializer extends FetchParentAccess { default boolean isEntityInitializer() { return true; } + + @Override + default EntityInitializer asEntityInitializer() { + return this; + } + } diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntityDelayedFetchImpl.java b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntityDelayedFetchImpl.java index c1fbe807c0..e5dff94c06 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntityDelayedFetchImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntityDelayedFetchImpl.java @@ -14,6 +14,7 @@ import org.hibernate.sql.results.graph.DomainResult; import org.hibernate.sql.results.graph.DomainResultAssembler; import org.hibernate.sql.results.graph.FetchParent; import org.hibernate.sql.results.graph.FetchParentAccess; +import org.hibernate.sql.results.graph.Initializer; import org.hibernate.sql.results.graph.entity.EntityInitializer; /** @@ -54,7 +55,7 @@ public class EntityDelayedFetchImpl extends AbstractNonJoinedEntityFetch { FetchParentAccess parentAccess, AssemblerCreationState creationState) { final NavigablePath navigablePath = getNavigablePath(); - final EntityInitializer entityInitializer = (EntityInitializer) creationState.resolveInitializer( + final Initializer entityInitializer = creationState.resolveInitializer( navigablePath, getEntityValuedModelPart(), () -> new EntityDelayedFetchInitializer( @@ -66,6 +67,6 @@ public class EntityDelayedFetchImpl extends AbstractNonJoinedEntityFetch { ) ); - return new EntityAssembler( getFetchedMapping().getJavaType(), entityInitializer ); + return new EntityAssembler( getFetchedMapping().getJavaType(), entityInitializer.asEntityInitializer() ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntityDelayedResultImpl.java b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntityDelayedResultImpl.java index 533f188be9..4d5b3e3395 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntityDelayedResultImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntityDelayedResultImpl.java @@ -16,6 +16,7 @@ import org.hibernate.sql.results.graph.DomainResult; import org.hibernate.sql.results.graph.DomainResultAssembler; import org.hibernate.sql.results.graph.DomainResultCreationState; import org.hibernate.sql.results.graph.FetchParentAccess; +import org.hibernate.sql.results.graph.Initializer; import org.hibernate.sql.results.graph.entity.EntityInitializer; import org.hibernate.type.descriptor.java.JavaType; @@ -64,7 +65,7 @@ public class EntityDelayedResultImpl implements DomainResult { public DomainResultAssembler createResultAssembler( FetchParentAccess parentAccess, AssemblerCreationState creationState) { - final EntityInitializer initializer = (EntityInitializer) creationState.resolveInitializer( + final Initializer initializer = creationState.resolveInitializer( getNavigablePath(), entityValuedModelPart, () -> new EntityDelayedFetchInitializer( @@ -76,7 +77,7 @@ public class EntityDelayedResultImpl implements DomainResult { ) ); - return new EntityAssembler( getResultJavaType(), initializer ); + return new EntityAssembler( getResultJavaType(), initializer.asEntityInitializer() ); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntityFetchJoinedImpl.java b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntityFetchJoinedImpl.java index 17576f199f..9f4a2bbdea 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntityFetchJoinedImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntityFetchJoinedImpl.java @@ -80,7 +80,7 @@ public class EntityFetchJoinedImpl extends AbstractNonLazyEntityFetch { protected EntityInitializer getEntityInitializer( FetchParentAccess parentAccess, AssemblerCreationState creationState) { - return (EntityInitializer) creationState.resolveInitializer( + return creationState.resolveInitializer( getNavigablePath(), getReferencedModePart(), () -> new EntityJoinedFetchInitializer( @@ -94,7 +94,7 @@ public class EntityFetchJoinedImpl extends AbstractNonLazyEntityFetch { entityResult.getDiscriminatorFetch(), creationState ) - ); + ).asEntityInitializer(); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntityFetchSelectImpl.java b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntityFetchSelectImpl.java index 4449b6c56d..ece4d4d7a9 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntityFetchSelectImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntityFetchSelectImpl.java @@ -16,6 +16,7 @@ import org.hibernate.sql.results.graph.DomainResultAssembler; import org.hibernate.sql.results.graph.DomainResultCreationState; import org.hibernate.sql.results.graph.FetchParent; import org.hibernate.sql.results.graph.FetchParentAccess; +import org.hibernate.sql.results.graph.Initializer; import org.hibernate.sql.results.graph.entity.EntityInitializer; /** @@ -52,7 +53,7 @@ public class EntityFetchSelectImpl extends AbstractNonJoinedEntityFetch { @Override public DomainResultAssembler createAssembler(FetchParentAccess parentAccess, AssemblerCreationState creationState) { - final EntityInitializer initializer = (EntityInitializer) creationState.resolveInitializer( + final Initializer initializer = creationState.resolveInitializer( getNavigablePath(), getFetchedMapping(), () -> { @@ -99,6 +100,6 @@ public class EntityFetchSelectImpl extends AbstractNonJoinedEntityFetch { } ); - return new EntityAssembler( getResultJavaType(), initializer ); + return new EntityAssembler( getResultJavaType(), initializer.asEntityInitializer() ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntityResultImpl.java b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntityResultImpl.java index 2f92f25da0..083faf8c93 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntityResultImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntityResultImpl.java @@ -19,6 +19,7 @@ import org.hibernate.sql.results.graph.DomainResultCreationState; import org.hibernate.sql.results.graph.FetchParentAccess; import org.hibernate.sql.results.graph.Fetchable; import org.hibernate.sql.results.graph.FetchableContainer; +import org.hibernate.sql.results.graph.Initializer; import org.hibernate.sql.results.graph.entity.AbstractEntityResultGraphNode; import org.hibernate.sql.results.graph.entity.EntityInitializer; import org.hibernate.sql.results.graph.entity.EntityResult; @@ -92,7 +93,7 @@ public class EntityResultImpl extends AbstractEntityResultGraphNode implements E public DomainResultAssembler createResultAssembler( FetchParentAccess parentAccess, AssemblerCreationState creationState) { - final EntityInitializer initializer = (EntityInitializer) creationState.resolveInitializer( + final Initializer initializer = creationState.resolveInitializer( getNavigablePath(), getReferencedModePart(), () -> new EntityResultInitializer( @@ -106,7 +107,7 @@ public class EntityResultImpl extends AbstractEntityResultGraphNode implements E ) ); - return new EntityAssembler( this.getResultJavaType(), initializer ); + return new EntityAssembler( this.getResultJavaType(), initializer.asEntityInitializer() ); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntityResultJoinedSubclassImpl.java b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntityResultJoinedSubclassImpl.java index 0f461a0ada..6264795f83 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntityResultJoinedSubclassImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntityResultJoinedSubclassImpl.java @@ -13,6 +13,7 @@ import org.hibernate.sql.results.graph.AssemblerCreationState; import org.hibernate.sql.results.graph.DomainResultAssembler; import org.hibernate.sql.results.graph.DomainResultCreationState; import org.hibernate.sql.results.graph.FetchParentAccess; +import org.hibernate.sql.results.graph.Initializer; import org.hibernate.sql.results.graph.entity.EntityInitializer; /** @@ -32,7 +33,7 @@ public class EntityResultJoinedSubclassImpl extends EntityResultImpl { public DomainResultAssembler createResultAssembler( FetchParentAccess parentAccess, AssemblerCreationState creationState) { - final EntityInitializer initializer = (EntityInitializer) creationState.resolveInitializer( + final Initializer initializer = creationState.resolveInitializer( getNavigablePath(), getReferencedModePart(), () -> new EntityResultInitializer( @@ -46,7 +47,7 @@ public class EntityResultJoinedSubclassImpl extends EntityResultImpl { ) ); - return new EntityAssembler( this.getResultJavaType(), initializer ); + return new EntityAssembler( this.getResultJavaType(), initializer.asEntityInitializer() ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/internal/domain/CircularBiDirectionalFetchImpl.java b/hibernate-core/src/main/java/org/hibernate/sql/results/internal/domain/CircularBiDirectionalFetchImpl.java index 6ec71a655f..b2cf9cd4cc 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/internal/domain/CircularBiDirectionalFetchImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/internal/domain/CircularBiDirectionalFetchImpl.java @@ -242,7 +242,7 @@ public class CircularBiDirectionalFetchImpl implements BiDirectionalFetch, Assoc path = path.getParent(); parentInitializer = rowProcessingState.resolveInitializer( path ); } - initializer = (EntityInitializer) parentInitializer; + initializer = parentInitializer.asEntityInitializer(); } else { final Initializer parentInitializer = rowProcessingState.resolveInitializer( circularPath ); @@ -319,14 +319,14 @@ public class CircularBiDirectionalFetchImpl implements BiDirectionalFetch, Assoc private EntityInitializer resolveCircularInitializer(RowProcessingState rowProcessingState) { final Initializer initializer = rowProcessingState.resolveInitializer( circularPath ); - if ( initializer.isEntityInitializer() ) { - return (EntityInitializer) initializer; + final EntityInitializer entityInitializer = initializer.asEntityInitializer(); + if ( entityInitializer!=null ) { + return entityInitializer; } if ( initializer.isCollectionInitializer() ) { return null; } final ModelPart initializedPart = initializer.getInitializedPart(); - if ( initializedPart instanceof EntityInitializer ) { return (EntityInitializer) initializedPart; } diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/internal/domain/CircularFetchImpl.java b/hibernate-core/src/main/java/org/hibernate/sql/results/internal/domain/CircularFetchImpl.java index 9da142d39c..13174f85c1 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/internal/domain/CircularFetchImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/internal/domain/CircularFetchImpl.java @@ -33,6 +33,7 @@ import org.hibernate.sql.results.graph.FetchOptions; import org.hibernate.sql.results.graph.FetchParent; import org.hibernate.sql.results.graph.FetchParentAccess; import org.hibernate.sql.results.graph.Fetchable; +import org.hibernate.sql.results.graph.Initializer; import org.hibernate.sql.results.graph.entity.EntityInitializer; import org.hibernate.sql.results.graph.entity.internal.BatchEntityInsideEmbeddableSelectFetchInitializer; import org.hibernate.sql.results.graph.entity.internal.BatchEntitySelectFetchInitializer; @@ -111,7 +112,7 @@ public class CircularFetchImpl implements BiDirectionalFetch, Association { AssemblerCreationState creationState) { final DomainResultAssembler keyAssembler = keyResult.createResultAssembler( parentAccess, creationState ); - final EntityInitializer initializer = (EntityInitializer) creationState.resolveInitializer( + final Initializer initializer = creationState.resolveInitializer( getNavigablePath(), referencedModelPart, () -> { @@ -167,7 +168,7 @@ public class CircularFetchImpl implements BiDirectionalFetch, Association { ); return new BiDirectionalFetchAssembler( - initializer, + initializer.asEntityInitializer(), fetchable.getJavaType() ); }