diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/DomainModelHelper.java b/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/DomainModelHelper.java index 259a394a8b..c1aef741a1 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/DomainModelHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/DomainModelHelper.java @@ -12,7 +12,9 @@ import org.hibernate.graph.spi.SubGraphImplementor; import org.hibernate.metamodel.MappingMetamodel; import org.hibernate.metamodel.mapping.EntityMappingType; import org.hibernate.metamodel.mapping.MappingModelHelper; +import org.hibernate.metamodel.mapping.ModelPart; import org.hibernate.metamodel.model.domain.EmbeddableDomainType; +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; @@ -61,18 +63,44 @@ public class DomainModelHelper { if ( attribute1 == attribute2 ) { return true; } - final MappingMetamodel mappingMetamodel = jpaMetamodel.getMappingMetamodel(); - final EntityMappingType entity1 = mappingMetamodel.getEntityDescriptor( - attribute1.getDeclaringType().getTypeName() + final MappingMetamodel runtimeMetamodels = jpaMetamodel.getMappingMetamodel(); + final ModelPart modelPart1 = getEntityAttributeModelPart( + attribute1, + attribute1.getDeclaringType(), + runtimeMetamodels ); - final EntityMappingType entity2 = mappingMetamodel.getEntityDescriptor( - attribute2.getDeclaringType().getTypeName() + final ModelPart modelPart2 = getEntityAttributeModelPart( + attribute2, + attribute2.getDeclaringType(), + runtimeMetamodels ); + return modelPart1 != null && modelPart2 != null && MappingModelHelper.isCompatibleModelPart( + modelPart1, + modelPart2 + ); + } - return entity1 != null && entity2 != null && MappingModelHelper.isCompatibleModelPart( - entity1.findSubPart( attribute1.getName() ), - entity2.findSubPart( attribute2.getName() ) - ); + static ModelPart getEntityAttributeModelPart( + PersistentAttribute attribute, + ManagedDomainType domainType, + MappingMetamodel mappingMetamodel) { + if ( domainType instanceof EntityDomainType ) { + final EntityMappingType entity = mappingMetamodel.getEntityDescriptor( domainType.getTypeName() ); + return entity.findSubPart( attribute.getName() ); + } + else { + ModelPart candidate = null; + for ( ManagedDomainType subType : domainType.getSubTypes() ) { + final ModelPart modelPart = getEntityAttributeModelPart( attribute, subType, mappingMetamodel ); + if ( modelPart != null ) { + if ( candidate != null && !MappingModelHelper.isCompatibleModelPart( candidate, modelPart ) ) { + return null; + } + candidate = modelPart; + } + } + return candidate; + } } public static ManagedDomainType findSubType(ManagedDomainType type, Class subtype) {