Skip to content

Commit

Permalink
HHH-17491 Fix checking subtype attribute declared in MappedSuperclass
Browse files Browse the repository at this point in the history
  • Loading branch information
mbladel committed Dec 1, 2023
1 parent 9ace529 commit b9ff974
Showing 1 changed file with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
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;
Expand Down Expand Up @@ -61,20 +63,46 @@ static boolean isCompatible(
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 entity1 != null && entity2 != null && MappingModelHelper.isCompatibleModelPart(
entity1.findSubPart( attribute1.getName() ),
entity2.findSubPart( attribute2.getName() )
return modelPart1 != null && modelPart2 != null && MappingModelHelper.isCompatibleModelPart(
modelPart1,
modelPart2
);
}

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) {
if ( type.getBindableJavaType() == subtype ) {
@SuppressWarnings("unchecked")
Expand Down

0 comments on commit b9ff974

Please sign in to comment.