Skip to content

Commit 1bef934

Browse files
committed
HHH-19756 Improve sub-part resolution when specifying a treat type
1 parent 1a6ce19 commit 1bef934

File tree

1 file changed

+44
-49
lines changed

1 file changed

+44
-49
lines changed

hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java

Lines changed: 44 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5660,49 +5660,30 @@ public ModelPart findSubPart(String name, EntityMappingType treatTargetType) {
56605660
}
56615661
}
56625662

5663-
if ( treatTargetType != null ) {
5664-
if ( ! treatTargetType.isTypeOrSuperType( this ) ) {
5663+
if ( treatTargetType == null ) {
5664+
final var subDefinedAttribute = findSubPartInSubclassMappings( name );
5665+
if ( subDefinedAttribute != null ) {
5666+
return subDefinedAttribute;
5667+
}
5668+
}
5669+
else if ( treatTargetType != this ) {
5670+
if ( !treatTargetType.isTypeOrSuperType( this ) ) {
56655671
return null;
56665672
}
5667-
5668-
if ( subclassMappingTypes != null && !subclassMappingTypes.isEmpty() ) {
5669-
for ( var subMappingType : subclassMappingTypes.values() ) {
5670-
if ( treatTargetType.isTypeOrSuperType( subMappingType ) ) {
5671-
final var subDefinedAttribute =
5672-
subMappingType.findSubTypesSubPart( name, treatTargetType );
5673-
if ( subDefinedAttribute != null ) {
5674-
return subDefinedAttribute;
5675-
}
5676-
}
5677-
}
5673+
// Prefer attributes defined in the treat target type or its subtypes
5674+
final var treatTypeSubPart = treatTargetType.findSubTypesSubPart( name, null );
5675+
if ( treatTypeSubPart != null ) {
5676+
return treatTypeSubPart;
56785677
}
5679-
}
5680-
else {
5681-
if ( subclassMappingTypes != null && !subclassMappingTypes.isEmpty() ) {
5682-
ModelPart attribute = null;
5683-
for ( EntityMappingType subMappingType : subclassMappingTypes.values() ) {
5684-
final var subDefinedAttribute =
5685-
subMappingType.findSubTypesSubPart( name, treatTargetType );
5686-
if ( subDefinedAttribute != null ) {
5687-
if ( attribute != null && !isCompatibleModelPart( attribute, subDefinedAttribute ) ) {
5688-
throw new PathException(
5689-
String.format(
5690-
Locale.ROOT,
5691-
"Could not resolve attribute '%s' of '%s' due to the attribute being declared in multiple subtypes '%s' and '%s'",
5692-
name,
5693-
getJavaType().getTypeName(),
5694-
attribute.asAttributeMapping().getDeclaringType()
5695-
.getJavaType().getTypeName(),
5696-
subDefinedAttribute.asAttributeMapping().getDeclaringType()
5697-
.getJavaType().getTypeName()
5698-
)
5699-
);
5700-
}
5701-
attribute = subDefinedAttribute;
5678+
else {
5679+
// If not found, look in the treat target type's supertypes
5680+
EntityMappingType superType = treatTargetType.getSuperMappingType();
5681+
while ( superType != this ) {
5682+
final var superTypeSubPart = superType.findDeclaredAttributeMapping( name );
5683+
if ( superTypeSubPart != null ) {
5684+
return superTypeSubPart;
57025685
}
5703-
}
5704-
if ( attribute != null ) {
5705-
return attribute;
5686+
superType = superType.getSuperMappingType();
57065687
}
57075688
}
57085689
}
@@ -5725,23 +5706,37 @@ public ModelPart findSubPart(String name, EntityMappingType treatTargetType) {
57255706
}
57265707
}
57275708

5709+
private ModelPart findSubPartInSubclassMappings(String name) {
5710+
ModelPart attribute = null;
5711+
if ( isNotEmpty( subclassMappingTypes ) ) {
5712+
for ( var subMappingType : subclassMappingTypes.values() ) {
5713+
final var subDefinedAttribute = subMappingType.findSubTypesSubPart( name, null );
5714+
if ( subDefinedAttribute != null ) {
5715+
if ( attribute != null && !isCompatibleModelPart( attribute, subDefinedAttribute ) ) {
5716+
throw new PathException( String.format(
5717+
Locale.ROOT,
5718+
"Could not resolve attribute '%s' of '%s' due to the attribute being declared in multiple subtypes '%s' and '%s'",
5719+
name,
5720+
getJavaType().getTypeName(),
5721+
attribute.asAttributeMapping().getDeclaringType().getJavaType().getTypeName(),
5722+
subDefinedAttribute.asAttributeMapping().getDeclaringType().getJavaType().getTypeName()
5723+
) );
5724+
}
5725+
attribute = subDefinedAttribute;
5726+
}
5727+
}
5728+
}
5729+
return attribute;
5730+
}
5731+
57285732
@Override
57295733
public ModelPart findSubTypesSubPart(String name, EntityMappingType treatTargetType) {
57305734
final var declaredAttribute = declaredAttributeMappings.get( name );
57315735
if ( declaredAttribute != null ) {
57325736
return declaredAttribute;
57335737
}
57345738
else {
5735-
if ( subclassMappingTypes != null && !subclassMappingTypes.isEmpty() ) {
5736-
for ( var subMappingType : subclassMappingTypes.values() ) {
5737-
final var subDefinedAttribute =
5738-
subMappingType.findSubTypesSubPart( name, treatTargetType );
5739-
if ( subDefinedAttribute != null ) {
5740-
return subDefinedAttribute;
5741-
}
5742-
}
5743-
}
5744-
return null;
5739+
return findSubPartInSubclassMappings( name );
57455740
}
57465741
}
57475742

0 commit comments

Comments
 (0)