@@ -5660,49 +5660,30 @@ public ModelPart findSubPart(String name, EntityMappingType treatTargetType) {
5660
5660
}
5661
5661
}
5662
5662
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 ) ) {
5665
5671
return null ;
5666
5672
}
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 ;
5678
5677
}
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 ;
5702
5685
}
5703
- }
5704
- if ( attribute != null ) {
5705
- return attribute ;
5686
+ superType = superType .getSuperMappingType ();
5706
5687
}
5707
5688
}
5708
5689
}
@@ -5725,23 +5706,37 @@ public ModelPart findSubPart(String name, EntityMappingType treatTargetType) {
5725
5706
}
5726
5707
}
5727
5708
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
+
5728
5732
@ Override
5729
5733
public ModelPart findSubTypesSubPart (String name , EntityMappingType treatTargetType ) {
5730
5734
final var declaredAttribute = declaredAttributeMappings .get ( name );
5731
5735
if ( declaredAttribute != null ) {
5732
5736
return declaredAttribute ;
5733
5737
}
5734
5738
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 );
5745
5740
}
5746
5741
}
5747
5742
0 commit comments