Context based mapping - Can't exclude parent field using a working map-id

121 views
Skip to first unread message

Luc Chevaux

unread,
Mar 15, 2017, 7:09:28 AM3/15/17
to Dozer Mapper
Hi all,

basing on this documentation I tried to use context based mapping to exclude certain parent field (province) on Municipality internal field mapping.
My main goal is to have a plain patient mapping without carrying forward all hibernate entities related to a specific field (in this case it means that I have also mapped region and country).

I tried also to use a map-id attribute on mapping tag and I also tried to repeat internal excluding field <field-exclude> on the main mapping definition with no success.
I added also a-hint and b-hint tags but nothing seems to work as expected.

Both mapping are working fine but province and related objects are never excluded from mapping. Mapping with map-id municipality-no-province works well when it's called alone.

This is the cleaned mapping that I would expected to work:

<mapping>
        <class-a>it.clevercom.echo.rd.model.entity.Patient</class-a>
        <class-b>it.clevercom.echo.rd.model.dto.PatientDTO</class-b>
       
        <field>
            <a>countryByDomicileidcountry</a>
            <b>docimicileCountry</b>
        </field>
       
        <field>
            <a>countryByResidenceidcountry</a>
            <b>residenceCountry</b>
        </field>
       
        <field>
            <a>countryByBirthplaceidcountry</a>
            <b>birthplaceCountry</b>
        </field>
       
        <field map-id="municipality-no-province">
            <a>municipalityByDomicileidmunicipality</a>
            <b>domicileMunicipality</b>
            <a-hint>it.clevercom.echo.rd.model.entity.Municipality</a-hint>
            <b-hint>it.clevercom.echo.rd.model.dto.MunicipalityDTO</b-hint>
        </field>
       
        <field map-id="municipality-no-province">
            <a>municipalityByResidenceidmunicipality</a>
            <b>residenceMunicipality</b>
            <a-hint>it.clevercom.echo.rd.model.entity.Municipality</a-hint>
            <b-hint>it.clevercom.echo.rd.model.dto.MunicipalityDTO</b-hint>
        </field>
       
        <field map-id="municipality-no-province">
            <a>municipalityByBirthplaceidmunicipality</a>
            <b>birthplaceMunicipality</b>
            <a-hint>it.clevercom.echo.rd.model.entity.Municipality</a-hint>
            <b-hint>it.clevercom.echo.rd.model.dto.MunicipalityDTO</b-hint>
        </field>
       
        <field>
            <a>organizationUnitByIdintorganizationunit</a>
            <b>internalOrgUnit</b>
        </field>
       
        <field>
            <a>organizationUnitByIdextorganizationunit</a>
            <b>externalOrgUnit</b>
        </field>
 </mapping>


<mapping map-id="municipality-no-province">
        <class-a>it.clevercom.echo.rd.model.entity.Municipality</class-a>
        <class-b>it.clevercom.echo.rd.model.dto.MunicipalityDTO</class-b>
        <field-exclude>
            <a>province</a>
            <b>province</b>
        </field-exclude>
    </mapping>

I have no idea of what is going wrong. Does somebody has an advice to make it work?
Thank u.

Cheers,
Luca

Luc Chevaux

unread,
Mar 15, 2017, 6:29:05 PM3/15/17
to Dozer Mapper
I finally identified the problem while debbugging very hard MappingProcessor.java:
This portion of code:

// Now check for super class mappings.  Process super class mappings first.
    if (!bypassSuperMappings) {
      Collection<ClassMap> superMappings = new ArrayList<ClassMap>();

      Collection<ClassMap> superClasses = checkForSuperTypeMapping(srcClass, destClass);
      //List<ClassMap> interfaceMappings = classMappings.findInterfaceMappings(srcClass, destClass);

      superMappings.addAll(superClasses);
      //superMappings.addAll(interfaceMappings);
      if (!superMappings.isEmpty()) {
        processSuperTypeMapping(superMappings, srcObj, destObj, mappedParentFields, mapId);
      }
    }

specially:

        processSuperTypeMapping(superMappings, srcObj, destObj, mappedParentFields, mapId);

Seems to reset fieldMaps collection inside input ClassMap object.
After that I noticed also that objects of types ExcludeFieldMap in that collection (in this case province) turns to GenericFieldMap.
That's why they never been considered while running the following code:

// Perform mappings for each field. Iterate through Fields Maps for this class mapping
    for (FieldMap fieldMapping : classMap.getFieldMaps()) {
      //Bypass field if it has already been mapped as part of super class mappings.
      String key = MappingUtils.getMappedParentFieldKey(destObj, fieldMapping);
      if (mappedParentFields != null && mappedParentFields.contains(key)) {
        continue;
      }
      mapField(fieldMapping, srcObj, destObj);
    }

Forcing the value of bypassSuperMappings in order to avoid the execution of method processSuperTypeMapping make it work as expected.

{
  "idpatient": 10,
  "citizenship": null,
  "docimicileCountry": null,
  "residenceCountry": null,
  "birthplaceCountry": null,
  "maritalStatus": null,
  "domicileMunicipality": null,
  "residenceMunicipality": {
    "idmunicipality": 1720,
    "municipalityname": "Roma",
    "municipalitystdcode": "058091",
    "postalcode": "00100"
  },

  "birthplaceMunicipality": null,
  "internalOrgUnit": null,
  "externalOrgUnit": null,
  "name": "Anna",
  "surname": "Bozzo",
  "dateofbirth": -746935200000,
  "gender": "F",
  "residencestreetaddress": null,
  "domicilestreetaddress": null,
  "deathdate": null,
  "taxcode": null,
  "phonenumber": "345345345",
  "email": null,
  "prettyPrint": "Anna Bozzo"
}

Maybe could be related to this missing functionality?

https://sourceforge.net/p/dozer/discussion/452530/thread/72589789/
Reply all
Reply to author
Forward
0 new messages