From d32e8adaf239aff7b9ccad5575f87245802a599f Mon Sep 17 00:00:00 2001 From: Gavin King Date: Sat, 1 Jul 2023 21:05:59 +0200 Subject: [PATCH] detach EntityGraphs from the JpaMetamodel so that they can be newed more easily --- .../graph/internal/AbstractGraph.java | 22 ++++--------- .../graph/internal/AbstractGraphNode.java | 10 ++---- .../graph/internal/AttributeNodeImpl.java | 19 +++--------- .../graph/internal/RootGraphImpl.java | 31 +++++++++---------- .../graph/internal/SubGraphImpl.java | 26 ++++++---------- .../graph/internal/parse/GraphParser.java | 2 -- .../hibernate/graph/spi/GraphImplementor.java | 6 ++-- .../graph/spi/RootGraphImplementor.java | 9 +++--- .../org/hibernate/internal/SessionImpl.java | 8 ++--- .../ast/internal/LoaderSelectBuilder.java | 16 +++++++--- .../model/domain/AbstractManagedType.java | 2 +- .../domain/internal/EmbeddableTypeImpl.java | 2 +- .../model/domain/internal/EntityTypeImpl.java | 2 +- .../domain/internal/JpaMetamodelImpl.java | 8 ++--- .../internal/MappedSuperclassTypeImpl.java | 2 +- .../entity/AbstractEntityPersister.java | 2 +- .../spi/AbstractCommonQueryContract.java | 2 +- .../sqm/sql/BaseSqmToSqlAstConverter.java | 4 ++- .../results/graph/DomainResultGraphNode.java | 3 +- .../sql/results/graph/FetchParent.java | 6 ++-- .../internal/EmbeddableFetchImpl.java | 5 +-- .../graph/entity/EntityResultGraphNode.java | 6 ++-- ...StandardEntityGraphTraversalStateImpl.java | 10 ++++-- .../orm/test/graph/EntityGraphsTest.java | 2 +- 24 files changed, 90 insertions(+), 115 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/graph/internal/AbstractGraph.java b/hibernate-core/src/main/java/org/hibernate/graph/internal/AbstractGraph.java index b86c27b523..f083e5e693 100644 --- a/hibernate-core/src/main/java/org/hibernate/graph/internal/AbstractGraph.java +++ b/hibernate-core/src/main/java/org/hibernate/graph/internal/AbstractGraph.java @@ -21,7 +21,6 @@ import org.hibernate.graph.spi.AttributeNodeImplementor; import org.hibernate.graph.spi.GraphImplementor; import org.hibernate.graph.spi.RootGraphImplementor; import org.hibernate.metamodel.model.domain.EntityDomainType; -import org.hibernate.metamodel.model.domain.JpaMetamodel; import org.hibernate.metamodel.model.domain.ManagedDomainType; import org.hibernate.metamodel.model.domain.PersistentAttribute; @@ -36,17 +35,13 @@ public abstract class AbstractGraph extends AbstractGraphNode implements G private final ManagedDomainType managedType; private Map, AttributeNodeImplementor> attrNodeMap; - public AbstractGraph( - ManagedDomainType managedType, - boolean mutable, - JpaMetamodel jpaMetamodel) { - super( mutable, jpaMetamodel ); + public AbstractGraph(ManagedDomainType managedType, boolean mutable) { + super( mutable ); this.managedType = managedType; } - protected AbstractGraph(boolean mutable, GraphImplementor original) { - this( original.getGraphedType(), mutable, original.jpaMetamodel() ); - + protected AbstractGraph(GraphImplementor original, boolean mutable) { + this( original.getGraphedType(), mutable ); this.attrNodeMap = new ConcurrentHashMap<>( original.getAttributeNodeList().size() ); original.visitAttributeNodes( node -> attrNodeMap.put( @@ -56,11 +51,6 @@ public abstract class AbstractGraph extends AbstractGraphNode implements G ); } - @Override - public JpaMetamodel jpaMetamodel() { - return super.jpaMetamodel(); - } - @Override public ManagedDomainType getGraphedType() { return managedType; @@ -69,7 +59,7 @@ public abstract class AbstractGraph extends AbstractGraphNode implements G @Override public RootGraphImplementor makeRootGraph(String name, boolean mutable) { if ( getGraphedType() instanceof EntityDomainType ) { - return new RootGraphImpl<>( name, mutable, this ); + return new RootGraphImpl<>( name, this, mutable); } throw new CannotBecomeEntityGraphException( @@ -180,7 +170,7 @@ public abstract class AbstractGraph extends AbstractGraphNode implements G } if ( attrNode == null ) { - attrNode = new AttributeNodeImpl<>( isMutable(), attribute, jpaMetamodel() ); + attrNode = new AttributeNodeImpl<>(attribute, isMutable()); attrNodeMap.put( attribute, attrNode ); } diff --git a/hibernate-core/src/main/java/org/hibernate/graph/internal/AbstractGraphNode.java b/hibernate-core/src/main/java/org/hibernate/graph/internal/AbstractGraphNode.java index 7a1accefbe..af727d647b 100644 --- a/hibernate-core/src/main/java/org/hibernate/graph/internal/AbstractGraphNode.java +++ b/hibernate-core/src/main/java/org/hibernate/graph/internal/AbstractGraphNode.java @@ -7,24 +7,18 @@ package org.hibernate.graph.internal; import org.hibernate.graph.spi.GraphNodeImplementor; -import org.hibernate.metamodel.model.domain.JpaMetamodel; /** * @author Steve Ebersole */ public abstract class AbstractGraphNode implements GraphNodeImplementor { - private final JpaMetamodel jpaMetamodel; + private final boolean mutable; - public AbstractGraphNode(boolean mutable, JpaMetamodel jpaMetamodel) { - this.jpaMetamodel = jpaMetamodel; + public AbstractGraphNode(boolean mutable) { this.mutable = mutable; } - protected JpaMetamodel jpaMetamodel() { - return jpaMetamodel; - } - @Override public boolean isMutable() { return mutable; diff --git a/hibernate-core/src/main/java/org/hibernate/graph/internal/AttributeNodeImpl.java b/hibernate-core/src/main/java/org/hibernate/graph/internal/AttributeNodeImpl.java index e49ab8a639..d121464889 100644 --- a/hibernate-core/src/main/java/org/hibernate/graph/internal/AttributeNodeImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/graph/internal/AttributeNodeImpl.java @@ -17,7 +17,6 @@ import org.hibernate.graph.spi.AttributeNodeImplementor; import org.hibernate.graph.spi.GraphImplementor; import org.hibernate.graph.spi.SubGraphImplementor; import org.hibernate.metamodel.model.domain.DomainType; -import org.hibernate.metamodel.model.domain.JpaMetamodel; import org.hibernate.metamodel.model.domain.ManagedDomainType; import org.hibernate.metamodel.model.domain.PersistentAttribute; import org.hibernate.metamodel.model.domain.SimpleDomainType; @@ -39,23 +38,19 @@ public class AttributeNodeImpl private Map, SubGraphImplementor> subGraphMap; private Map, SubGraphImplementor> keySubGraphMap; - public AttributeNodeImpl( - boolean mutable, - PersistentAttribute attribute, - JpaMetamodel jpaMetamodel) { - this( mutable, attribute, null, null, jpaMetamodel ); + public AttributeNodeImpl(PersistentAttribute attribute, boolean mutable) { + this(attribute, null, null, mutable); } /** * Intended only for use from making a copy */ private AttributeNodeImpl( - boolean mutable, PersistentAttribute attribute, Map, SubGraphImplementor> subGraphMap, Map, SubGraphImplementor> keySubGraphMap, - JpaMetamodel jpaMetamodel) { - super( mutable, jpaMetamodel ); + boolean mutable) { + super( mutable ); this.attribute = attribute; this.subGraphMap = subGraphMap; this.keySubGraphMap = keySubGraphMap; @@ -224,11 +219,7 @@ public class AttributeNodeImpl @Override public AttributeNodeImplementor makeCopy(boolean mutable) { return new AttributeNodeImpl<>( - mutable, - this.attribute, - makeMapCopy( mutable, subGraphMap ), - makeMapCopy( mutable, keySubGraphMap ), - jpaMetamodel() + this.attribute, makeMapCopy( mutable, subGraphMap ), makeMapCopy( mutable, keySubGraphMap ), mutable ); } diff --git a/hibernate-core/src/main/java/org/hibernate/graph/internal/RootGraphImpl.java b/hibernate-core/src/main/java/org/hibernate/graph/internal/RootGraphImpl.java index 7ab00a11f0..8f85dd9696 100644 --- a/hibernate-core/src/main/java/org/hibernate/graph/internal/RootGraphImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/graph/internal/RootGraphImpl.java @@ -23,23 +23,20 @@ import jakarta.persistence.EntityGraph; * @author Steve Ebersole */ public class RootGraphImpl extends AbstractGraph implements EntityGraph, RootGraphImplementor { + private final String name; - public RootGraphImpl( - String name, - EntityDomainType entityType, - boolean mutable, - JpaMetamodel jpaMetamodel) { - super( entityType, mutable, jpaMetamodel ); + public RootGraphImpl(String name, EntityDomainType entityType, boolean mutable) { + super( entityType, mutable ); this.name = name; } - public RootGraphImpl(String name, EntityDomainType entityType, JpaMetamodel jpaMetamodel) { - this( name, entityType, true, jpaMetamodel ); + public RootGraphImpl(String name, EntityDomainType entityType) { + this( name, entityType, true ); } - public RootGraphImpl(String name, boolean mutable, GraphImplementor original) { - super( mutable, original ); + public RootGraphImpl(String name, GraphImplementor original, boolean mutable) { + super(original, mutable); this.name = name; } @@ -50,12 +47,12 @@ public class RootGraphImpl extends AbstractGraph implements EntityGraph @Override public RootGraphImplementor makeCopy(boolean mutable) { - return new RootGraphImpl<>( null, mutable, this ); + return new RootGraphImpl<>( null, this, mutable); } @Override public SubGraphImplementor makeSubGraph(boolean mutable) { - return new SubGraphImpl<>( mutable, this ); + return new SubGraphImpl<>(this, mutable); } @Override @@ -69,7 +66,7 @@ public class RootGraphImpl extends AbstractGraph implements EntityGraph } @Override - public boolean appliesTo(EntityDomainType entityType) { + public boolean appliesTo(EntityDomainType entityType, JpaMetamodel metamodel) { final ManagedDomainType managedTypeDescriptor = getGraphedType(); if ( managedTypeDescriptor.equals( entityType ) ) { return true; @@ -87,12 +84,12 @@ public class RootGraphImpl extends AbstractGraph implements EntityGraph } @Override - public boolean appliesTo(String entityName) { - return appliesTo( jpaMetamodel().entity( entityName ) ); + public boolean appliesTo(String entityName, JpaMetamodel metamodel) { + return appliesTo( metamodel.entity( entityName ), metamodel ); } @Override - public boolean appliesTo(Class type) { - return appliesTo( jpaMetamodel().entity( type ) ); + public boolean appliesTo(Class type, JpaMetamodel metamodel) { + return appliesTo( metamodel.entity( type ), metamodel ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/graph/internal/SubGraphImpl.java b/hibernate-core/src/main/java/org/hibernate/graph/internal/SubGraphImpl.java index f850c85e58..632521ecf8 100644 --- a/hibernate-core/src/main/java/org/hibernate/graph/internal/SubGraphImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/graph/internal/SubGraphImpl.java @@ -14,29 +14,23 @@ import org.hibernate.metamodel.model.domain.ManagedDomainType; * @author Steve Ebersole */ public class SubGraphImpl extends AbstractGraph implements SubGraphImplementor { - public SubGraphImpl( - ManagedDomainType managedType, - boolean mutable, - JpaMetamodel jpaMetamodel) { - super( managedType, mutable, jpaMetamodel ); + + public SubGraphImpl(ManagedDomainType managedType, boolean mutable) { + super( managedType, mutable ); } - public SubGraphImpl(boolean mutable, AbstractGraph original) { - super( mutable, original ); + public SubGraphImpl(AbstractGraph original, boolean mutable) { + super(original, mutable); } @Override public SubGraphImplementor makeCopy(boolean mutable) { - return new SubGraphImpl<>( mutable, this ); + return new SubGraphImpl<>(this, mutable); } @Override public SubGraphImplementor makeSubGraph(boolean mutable) { - if ( ! mutable && ! isMutable() ) { - return this; - } - - return makeCopy( true ); + return !mutable && !isMutable() ? this : makeCopy( true ); } @Override @@ -45,7 +39,7 @@ public class SubGraphImpl extends AbstractGraph implements SubGraphImpleme } @Override - public boolean appliesTo(ManagedDomainType managedType) { + public boolean appliesTo(ManagedDomainType managedType, JpaMetamodel metamodel) { if ( getGraphedType().equals( managedType ) ) { return true; } @@ -62,7 +56,7 @@ public class SubGraphImpl extends AbstractGraph implements SubGraphImpleme } @Override - public boolean appliesTo(Class javaType) { - return appliesTo( jpaMetamodel().managedType( javaType ) ); + public boolean appliesTo(Class javaType, JpaMetamodel metamodel) { + return appliesTo( metamodel.managedType( javaType ), metamodel ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/graph/internal/parse/GraphParser.java b/hibernate-core/src/main/java/org/hibernate/graph/internal/parse/GraphParser.java index 83adf17c5a..6ea49b3ea0 100644 --- a/hibernate-core/src/main/java/org/hibernate/graph/internal/parse/GraphParser.java +++ b/hibernate-core/src/main/java/org/hibernate/graph/internal/parse/GraphParser.java @@ -18,8 +18,6 @@ import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.collections.Stack; import org.hibernate.internal.util.collections.StandardStack; -import org.jboss.logging.Logger; - import org.antlr.v4.runtime.CharStreams; import org.antlr.v4.runtime.CommonTokenStream; diff --git a/hibernate-core/src/main/java/org/hibernate/graph/spi/GraphImplementor.java b/hibernate-core/src/main/java/org/hibernate/graph/spi/GraphImplementor.java index 22910269ee..073d4586dd 100644 --- a/hibernate-core/src/main/java/org/hibernate/graph/spi/GraphImplementor.java +++ b/hibernate-core/src/main/java/org/hibernate/graph/spi/GraphImplementor.java @@ -26,14 +26,12 @@ import org.hibernate.metamodel.model.domain.PersistentAttribute; */ public interface GraphImplementor extends Graph, GraphNodeImplementor { - boolean appliesTo(ManagedDomainType managedType); + boolean appliesTo(ManagedDomainType managedType, JpaMetamodel metamodel); - boolean appliesTo(Class javaType); + boolean appliesTo(Class javaType, JpaMetamodel metamodel); void merge(GraphImplementor other); - JpaMetamodel jpaMetamodel(); - // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Co-variant returns diff --git a/hibernate-core/src/main/java/org/hibernate/graph/spi/RootGraphImplementor.java b/hibernate-core/src/main/java/org/hibernate/graph/spi/RootGraphImplementor.java index 8a2cb2a093..972741201e 100644 --- a/hibernate-core/src/main/java/org/hibernate/graph/spi/RootGraphImplementor.java +++ b/hibernate-core/src/main/java/org/hibernate/graph/spi/RootGraphImplementor.java @@ -8,6 +8,7 @@ package org.hibernate.graph.spi; import org.hibernate.graph.RootGraph; import org.hibernate.metamodel.model.domain.EntityDomainType; +import org.hibernate.metamodel.model.domain.JpaMetamodel; import org.hibernate.metamodel.model.domain.ManagedDomainType; /** @@ -17,14 +18,14 @@ import org.hibernate.metamodel.model.domain.ManagedDomainType; */ public interface RootGraphImplementor extends RootGraph, GraphImplementor { - boolean appliesTo(String entityName); + boolean appliesTo(String entityName, JpaMetamodel metamodel); - boolean appliesTo(EntityDomainType entityType); + boolean appliesTo(EntityDomainType entityType, JpaMetamodel metamodel); @Override - default boolean appliesTo(ManagedDomainType managedType) { + default boolean appliesTo(ManagedDomainType managedType, JpaMetamodel metamodel) { assert managedType instanceof EntityDomainType; - return appliesTo( (EntityDomainType) managedType ); + return appliesTo( (EntityDomainType) managedType, metamodel ); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java index d868f8b3eb..e219b62319 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java @@ -1064,7 +1064,7 @@ public class SessionImpl final GraphSemantic semantic = effectiveEntityGraph.getSemantic(); final RootGraphImplementor graph = effectiveEntityGraph.getGraph(); boolean clearedEffectiveGraph; - if ( semantic == null || graph.appliesTo( entityName ) ) { + if ( semantic == null || graph.appliesTo( entityName, getFactory().getJpaMetamodel() ) ) { clearedEffectiveGraph = false; } else { @@ -2862,11 +2862,7 @@ public class SessionImpl @Override public RootGraphImplementor createEntityGraph(Class rootType) { checkOpen(); - return new RootGraphImpl<>( - null, - getFactory().getJpaMetamodel().entity( rootType ), - getEntityManagerFactory().getJpaMetamodel() - ); + return new RootGraphImpl<>( null, getFactory().getJpaMetamodel().entity( rootType ) ); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/LoaderSelectBuilder.java b/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/LoaderSelectBuilder.java index 77b6d4da9c..acd1c883b7 100644 --- a/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/LoaderSelectBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/LoaderSelectBuilder.java @@ -130,7 +130,7 @@ public class LoaderSelectBuilder { 1, loadQueryInfluencers, lockOptions, - determineGraphTraversalState( loadQueryInfluencers ), + determineGraphTraversalState( loadQueryInfluencers, sessionFactory ), true, jdbcParameterConsumer ); @@ -157,7 +157,7 @@ public class LoaderSelectBuilder { -1, influencers, lockOptions, - determineGraphTraversalState( influencers ), + determineGraphTraversalState( influencers, sessionFactory ), true, null ); @@ -385,7 +385,7 @@ public class LoaderSelectBuilder { numberOfKeysToLoad, loadQueryInfluencers, lockOptions != null ? lockOptions : LockOptions.NONE, - determineGraphTraversalState( loadQueryInfluencers ), + determineGraphTraversalState( loadQueryInfluencers, creationContext.getSessionFactory() ), determineWhetherToForceIdSelection( numberOfKeysToLoad, restrictedParts ), jdbcParameterConsumer ); @@ -435,14 +435,20 @@ public class LoaderSelectBuilder { return false; } - private static EntityGraphTraversalState determineGraphTraversalState(LoadQueryInfluencers loadQueryInfluencers) { + private static EntityGraphTraversalState determineGraphTraversalState( + LoadQueryInfluencers loadQueryInfluencers, + SessionFactoryImplementor sessionFactory) { if ( loadQueryInfluencers != null ) { final EffectiveEntityGraph effectiveEntityGraph = loadQueryInfluencers.getEffectiveEntityGraph(); if ( effectiveEntityGraph != null ) { final GraphSemantic graphSemantic = effectiveEntityGraph.getSemantic(); final RootGraphImplementor rootGraphImplementor = effectiveEntityGraph.getGraph(); if ( graphSemantic != null && rootGraphImplementor != null ) { - return new StandardEntityGraphTraversalStateImpl( graphSemantic, rootGraphImplementor ); + return new StandardEntityGraphTraversalStateImpl( + graphSemantic, + rootGraphImplementor, + sessionFactory.getJpaMetamodel() + ); } } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/AbstractManagedType.java b/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/AbstractManagedType.java index bd746f95a9..399052dfad 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/AbstractManagedType.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/AbstractManagedType.java @@ -684,7 +684,7 @@ public abstract class AbstractManagedType @Override public SubGraphImplementor makeSubGraph() { - return new SubGraphImpl<>( this, true, jpaMetamodel() ); + return new SubGraphImpl<>( this, true); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/EmbeddableTypeImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/EmbeddableTypeImpl.java index 543de1b857..6117aa8145 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/EmbeddableTypeImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/EmbeddableTypeImpl.java @@ -44,6 +44,6 @@ public class EmbeddableTypeImpl @Override @SuppressWarnings("unchecked") public SubGraphImplementor makeSubGraph(Class subType) { - return new SubGraphImpl( this, true, jpaMetamodel() ); + return new SubGraphImpl( this, true ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/EntityTypeImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/EntityTypeImpl.java index cea2a9404a..f6138a6adf 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/EntityTypeImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/EntityTypeImpl.java @@ -212,7 +212,7 @@ public class EntityTypeImpl ); } - return new SubGraphImpl( this, true, jpaMetamodel() ); + return new SubGraphImpl( this, true ); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/JpaMetamodelImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/JpaMetamodelImpl.java index fa330baa6f..f27c6e40de 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/JpaMetamodelImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/JpaMetamodelImpl.java @@ -277,7 +277,7 @@ public class JpaMetamodelImpl implements JpaMetamodelImplementor, Serializable { //noinspection unchecked final RootGraphImplementor egi = (RootGraphImplementor) entityGraph; - if ( egi.appliesTo( entityType ) ) { + if ( egi.appliesTo( entityType, this ) ) { results.add( egi ); } } @@ -351,11 +351,7 @@ public class JpaMetamodelImpl implements JpaMetamodelImplementor, Serializable { ); } - final RootGraphImpl entityGraph = new RootGraphImpl<>( - definition.getRegisteredName(), - entityType, - this - ); + final RootGraphImpl entityGraph = new RootGraphImpl<>( definition.getRegisteredName(), entityType ); final NamedEntityGraph namedEntityGraph = definition.getAnnotation(); diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/MappedSuperclassTypeImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/MappedSuperclassTypeImpl.java index 0cd89001b0..22053a7277 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/MappedSuperclassTypeImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/MappedSuperclassTypeImpl.java @@ -130,7 +130,7 @@ public class MappedSuperclassTypeImpl extends AbstractIdentifiableType imp ); } - return new SubGraphImpl( this, true, jpaMetamodel() ); + return new SubGraphImpl( this, true ); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java index 862443f173..92d5df5842 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java @@ -3617,7 +3617,7 @@ public abstract class AbstractEntityPersister @Override public boolean isAffectedByEntityGraph(LoadQueryInfluencers loadQueryInfluencers) { final RootGraphImplementor graph = loadQueryInfluencers.getEffectiveEntityGraph().getGraph(); - return graph != null && graph.appliesTo( getEntityName() ); + return graph != null && graph.appliesTo( getEntityName(), getFactory().getJpaMetamodel() ); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/query/spi/AbstractCommonQueryContract.java b/hibernate-core/src/main/java/org/hibernate/query/spi/AbstractCommonQueryContract.java index 6fc2495ea7..66bcc33e96 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/spi/AbstractCommonQueryContract.java +++ b/hibernate-core/src/main/java/org/hibernate/query/spi/AbstractCommonQueryContract.java @@ -401,7 +401,7 @@ public abstract class AbstractCommonQueryContract implements CommonQueryContract final String entityName = runtimeMetamodels.getImportedName( graphString.substring( 0, separatorPosition ).trim() ); final String graphNodes = graphString.substring( separatorPosition + 1, terminatorPosition ); - final RootGraphImpl rootGraph = new RootGraphImpl<>( null, jpaMetamodel.entity( entityName ), jpaMetamodel ); + final RootGraphImpl rootGraph = new RootGraphImpl<>( null, jpaMetamodel.entity( entityName ) ); GraphParser.parseInto( (EntityGraph) rootGraph, graphNodes, getSession().getSessionFactory() ); applyGraph( rootGraph, graphSemantic ); } diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java index 297e31f38f..5d27a82f8c 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java @@ -562,7 +562,9 @@ public abstract class BaseSqmToSqlAstConverter extends Base final AppliedGraph appliedGraph = queryOptions.getAppliedGraph(); if ( appliedGraph != null && appliedGraph.getSemantic() != null && appliedGraph.getGraph() != null ) { this.entityGraphTraversalState = new StandardEntityGraphTraversalStateImpl( - appliedGraph.getSemantic(), appliedGraph.getGraph() ); + appliedGraph.getSemantic(), appliedGraph.getGraph(), + creationContext.getSessionFactory().getJpaMetamodel() + ); } else { this.entityGraphTraversalState = null; diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/DomainResultGraphNode.java b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/DomainResultGraphNode.java index 8c0b825c03..9930273374 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/DomainResultGraphNode.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/DomainResultGraphNode.java @@ -8,6 +8,7 @@ package org.hibernate.sql.results.graph; import org.hibernate.Incubating; import org.hibernate.graph.spi.GraphImplementor; +import org.hibernate.metamodel.model.domain.JpaMetamodel; import org.hibernate.spi.NavigablePath; import org.hibernate.type.descriptor.java.JavaType; @@ -41,7 +42,7 @@ public interface DomainResultGraphNode { return null; } - default boolean appliesTo(GraphImplementor graphImplementor){ + default boolean appliesTo(GraphImplementor graphImplementor, JpaMetamodel metamodel){ return false; } diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/FetchParent.java b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/FetchParent.java index eda07c1e74..4cafb9a61d 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/FetchParent.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/FetchParent.java @@ -58,7 +58,9 @@ public interface FetchParent extends DomainResultGraphNode { return getNavigablePath().treatAs( fetchableEntityType.getEntityName() ) .append( fetchableName ); } - return getNavigablePath().append( fetchableName ); + else { + return getNavigablePath().append( fetchableName ); + } } } @@ -66,7 +68,7 @@ public interface FetchParent extends DomainResultGraphNode { * Whereas {@link #getReferencedMappingContainer} and {@link #getReferencedMappingType} return the * referenced container type, this method returns the referenced part. * - * E.g. for a many-to-one this methods returns the + * E.g. for a many-to-one this method returns the * {@link ToOneAttributeMapping} while * {@link #getReferencedMappingContainer} and {@link #getReferencedMappingType} return the referenced * {@link org.hibernate.metamodel.mapping.EntityMappingType}. 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 b3dea9dc6d..9bfebeea71 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 @@ -10,6 +10,7 @@ import org.hibernate.engine.FetchTiming; import org.hibernate.graph.spi.GraphImplementor; import org.hibernate.metamodel.mapping.EmbeddableValuedModelPart; import org.hibernate.metamodel.mapping.EmbeddableMappingType; +import org.hibernate.metamodel.model.domain.JpaMetamodel; import org.hibernate.spi.NavigablePath; import org.hibernate.sql.ast.SqlAstJoinType; import org.hibernate.sql.ast.tree.from.TableGroup; @@ -160,8 +161,8 @@ public class EmbeddableFetchImpl extends AbstractFetchParent implements Embeddab } @Override - public boolean appliesTo(GraphImplementor graphImplementor) { - return getFetchParent().appliesTo( graphImplementor ); + public boolean appliesTo(GraphImplementor graphImplementor, JpaMetamodel metamodel) { + return getFetchParent().appliesTo( graphImplementor, metamodel ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/EntityResultGraphNode.java b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/EntityResultGraphNode.java index 45b60b0d43..5931b15558 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/EntityResultGraphNode.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/EntityResultGraphNode.java @@ -8,6 +8,7 @@ package org.hibernate.sql.results.graph.entity; import org.hibernate.graph.spi.GraphImplementor; import org.hibernate.metamodel.mapping.EntityMappingType; +import org.hibernate.metamodel.model.domain.JpaMetamodel; import org.hibernate.sql.results.graph.DomainResultGraphNode; import org.hibernate.sql.results.graph.FetchParent; import org.hibernate.metamodel.mapping.EntityValuedModelPart; @@ -41,9 +42,10 @@ public interface EntityResultGraphNode extends DomainResultGraphNode, FetchParen } @Override - default boolean appliesTo(GraphImplementor graphImplementor) { + default boolean appliesTo(GraphImplementor graphImplementor, JpaMetamodel metamodel) { return graphImplementor.appliesTo( - getEntityValuedModelPart().getEntityMappingType().getJavaType().getJavaTypeClass() + getEntityValuedModelPart().getEntityMappingType().getJavaType().getJavaTypeClass(), + metamodel ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/internal/StandardEntityGraphTraversalStateImpl.java b/hibernate-core/src/main/java/org/hibernate/sql/results/internal/StandardEntityGraphTraversalStateImpl.java index 19b572d6f4..27e6475414 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/internal/StandardEntityGraphTraversalStateImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/internal/StandardEntityGraphTraversalStateImpl.java @@ -19,6 +19,7 @@ import org.hibernate.metamodel.mapping.CollectionPart; import org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping; import org.hibernate.metamodel.mapping.PluralAttributeMapping; import org.hibernate.metamodel.mapping.internal.EntityCollectionPart; +import org.hibernate.metamodel.model.domain.JpaMetamodel; import org.hibernate.sql.results.graph.EntityGraphTraversalState; import org.hibernate.sql.results.graph.FetchParent; import org.hibernate.sql.results.graph.Fetchable; @@ -29,13 +30,18 @@ import org.hibernate.sql.results.graph.Fetchable; public class StandardEntityGraphTraversalStateImpl implements EntityGraphTraversalState { private final GraphSemantic graphSemantic; + private final JpaMetamodel metamodel; private GraphImplementor currentGraphContext; - public StandardEntityGraphTraversalStateImpl(GraphSemantic graphSemantic, RootGraphImplementor rootGraphImplementor) { + public StandardEntityGraphTraversalStateImpl( + GraphSemantic graphSemantic, + RootGraphImplementor rootGraphImplementor, + JpaMetamodel metamodel) { Objects.requireNonNull( graphSemantic, "graphSemantic cannot be null" ); Objects.requireNonNull( rootGraphImplementor, "rootGraphImplementor cannot be null" ); this.graphSemantic = graphSemantic; this.currentGraphContext = rootGraphImplementor; + this.metamodel = metamodel; } @Override @@ -101,7 +107,7 @@ public class StandardEntityGraphTraversalStateImpl implements EntityGraphTravers } private boolean appliesTo(FetchParent fetchParent) { - return currentGraphContext != null && fetchParent.appliesTo( currentGraphContext ); + return currentGraphContext != null && fetchParent.appliesTo( currentGraphContext, metamodel ); } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/graph/EntityGraphsTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/graph/EntityGraphsTest.java index 6248e05938..8922428e62 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/graph/EntityGraphsTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/graph/EntityGraphsTest.java @@ -198,6 +198,6 @@ public class EntityGraphsTest extends AbstractEntityGraphTest { @TestForIssue( jiraKey = "HHH-14264" ) public void testRootGraphAppliesToChildEntityClass() { RootGraphImplementor rootGraphImplementor = parseGraph( GraphParsingTestEntity.class, "name, description" ); - Assert.assertTrue( rootGraphImplementor.appliesTo( GraphParsingTestSubentity.class ) ); + Assert.assertTrue( rootGraphImplementor.appliesTo( GraphParsingTestSubentity.class, entityManagerFactory().getJpaMetamodel() ) ); } }