HHH-15810 Avoid secondary super-type cache pollution when casting to EntityInitializer

This commit is contained in:
Sanne Grinovero 2022-12-03 21:46:53 +00:00 committed by Sanne Grinovero
parent 8ebf3c8507
commit a7c3455274
14 changed files with 47 additions and 30 deletions

View File

@ -53,10 +53,7 @@ public class FetchingScrollableResultsImpl<R> extends AbstractScrollableResults<
private static <R> EntityInitializer extractResultInitializer(RowReader<R> 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

View File

@ -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() );
}
}

View File

@ -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();

View File

@ -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;
}
}

View File

@ -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() );

View File

@ -54,4 +54,10 @@ public interface EntityInitializer extends FetchParentAccess {
default boolean isEntityInitializer() {
return true;
}
@Override
default EntityInitializer asEntityInitializer() {
return this;
}
}

View File

@ -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() );
}
}

View File

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

View File

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

View File

@ -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() );
}
}

View File

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

View File

@ -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() );
}
}

View File

@ -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;
}

View File

@ -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()
);
}