HHH-17491 Fix checking subtype attribute declared in MappedSuperclass

This commit is contained in:
Marco Belladelli 2023-11-30 21:18:14 +01:00
parent 9ace529732
commit b9ff9744a3
1 changed files with 37 additions and 9 deletions

View File

@ -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 <J, S> ManagedDomainType<S> findSubType(ManagedDomainType<J> type, Class<S> subtype) {