From cc99b8a67c1355ec37cfdedf7944fc68828783ca Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Thu, 5 Oct 2023 10:49:58 +0200 Subject: [PATCH] HHH-17193 Introduce `Fetch#asFetchParent()` to avoid instanceof checks --- .../mapping/internal/DiscriminatedAssociationMapping.java | 5 +++++ .../main/java/org/hibernate/sql/results/graph/Fetch.java | 8 ++++++++ .../java/org/hibernate/sql/results/graph/FetchList.java | 2 +- .../graph/collection/internal/EagerCollectionFetch.java | 5 +++++ .../embeddable/internal/AggregateEmbeddableFetchImpl.java | 5 +++++ .../graph/embeddable/internal/EmbeddableFetchImpl.java | 4 ++++ .../graph/entity/AbstractEntityResultGraphNode.java | 6 +++--- .../hibernate/sql/results/graph/entity/EntityFetch.java | 5 +++++ 8 files changed, 36 insertions(+), 4 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/DiscriminatedAssociationMapping.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/DiscriminatedAssociationMapping.java index 85500909ad..e43605046a 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/DiscriminatedAssociationMapping.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/DiscriminatedAssociationMapping.java @@ -536,6 +536,11 @@ public class DiscriminatedAssociationMapping implements MappingType, FetchOption getKeyValueFetch().createAssembler( parentAccess, creationState ) ); } + + @Override + public FetchParent asFetchParent() { + return this; + } } private static class AnyResultAssembler implements DomainResultAssembler { diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/Fetch.java b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/Fetch.java index 0e5422fe7e..860e565895 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/Fetch.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/Fetch.java @@ -37,6 +37,14 @@ public interface Fetch extends DomainResultGraphNode { */ FetchParent getFetchParent(); + /** + * Utility method to avoid {@code instanceof} checks. Returns this if it's + * an instance of {@link FetchParent}, null otherwise. + */ + default FetchParent asFetchParent() { + return null; + } + /** * The value mapping being fetched */ diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/FetchList.java b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/FetchList.java index 273a0dd4b4..ff15c15b56 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/FetchList.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/FetchList.java @@ -72,7 +72,7 @@ public interface FetchList extends Iterable { if ( fetch instanceof EagerCollectionFetch ) { return true; } - else if ( fetch instanceof FetchParent && ( (FetchParent) fetch ).containsCollectionFetches() ) { + else if ( fetch.asFetchParent() != null && fetch.asFetchParent().containsCollectionFetches() ) { return true; } } diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/collection/internal/EagerCollectionFetch.java b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/collection/internal/EagerCollectionFetch.java index 03a8115c50..96a099004a 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/collection/internal/EagerCollectionFetch.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/collection/internal/EagerCollectionFetch.java @@ -220,4 +220,9 @@ public class EagerCollectionFetch extends CollectionFetch implements FetchParent public JavaType getResultJavaType() { return getFetchedMapping().getJavaType(); } + + @Override + public FetchParent asFetchParent() { + return this; + } } diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/embeddable/internal/AggregateEmbeddableFetchImpl.java b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/embeddable/internal/AggregateEmbeddableFetchImpl.java index c1cf091881..d566d3b7ed 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/embeddable/internal/AggregateEmbeddableFetchImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/embeddable/internal/AggregateEmbeddableFetchImpl.java @@ -170,4 +170,9 @@ public class AggregateEmbeddableFetchImpl extends AbstractFetchParent implements return new EmbeddableAssembler( initializer ); } + + @Override + public FetchParent asFetchParent() { + return this; + } } diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/embeddable/internal/EmbeddableFetchImpl.java b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/embeddable/internal/EmbeddableFetchImpl.java index fe8ccc7d40..455753a574 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/embeddable/internal/EmbeddableFetchImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/embeddable/internal/EmbeddableFetchImpl.java @@ -168,4 +168,8 @@ public class EmbeddableFetchImpl extends AbstractFetchParent implements Embeddab return getFetchParent().appliesTo( graphImplementor, metamodel ); } + @Override + public FetchParent asFetchParent() { + return this; + } } diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/AbstractEntityResultGraphNode.java b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/AbstractEntityResultGraphNode.java index 9621a28529..fa53ca6087 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/AbstractEntityResultGraphNode.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/AbstractEntityResultGraphNode.java @@ -43,13 +43,13 @@ public abstract class AbstractEntityResultGraphNode extends AbstractFetchParent final TableGroup entityTableGroup = creationState.getSqlAstCreationState().getFromClauseAccess() .getTableGroup( navigablePath ); final EntityResultGraphNode entityResultGraphNode = (EntityResultGraphNode) fetchParent; - final Fetch fetch = creationState.visitIdentifierFetch( entityResultGraphNode ); + final Fetch idFetch = creationState.visitIdentifierFetch( entityResultGraphNode ); if ( navigablePath.getParent() == null && !creationState.forceIdentifierSelection() && - ( !( fetch instanceof FetchParent ) || !( (FetchParent) fetch ).containsCollectionFetches() ) ) { + ( idFetch.asFetchParent() == null || !idFetch.asFetchParent().containsCollectionFetches() ) ) { identifierFetch = null; } else { - identifierFetch = fetch; + identifierFetch = idFetch; } discriminatorFetch = creationState.visitDiscriminatorFetch( entityResultGraphNode ); diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/EntityFetch.java b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/EntityFetch.java index a08133332c..160f4098e7 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/EntityFetch.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/EntityFetch.java @@ -7,6 +7,7 @@ package org.hibernate.sql.results.graph.entity; import org.hibernate.sql.results.graph.Fetch; +import org.hibernate.sql.results.graph.FetchParent; /** * Specialization of Fetch for entity-valued fetches @@ -19,4 +20,8 @@ public interface EntityFetch extends EntityResultGraphNode, Fetch { return true; } + @Override + default FetchParent asFetchParent() { + return this; + } }