In meantime i found a solution: First i ignore the field in question and map it in "AfterMapping". In my case i want to map also the child independently. In my case the @Context
only contains a Boolean
(boolean
is not working) that tells where i entered the cycle (startedFromPru
pru is the parent):
Mapper for parent (contains set of licenses):
@Mapper(componentModel = "spring", uses = {DateTimeMapper.class, LicenseMapper.class, CompanyCodeMapper.class, ProductHierarchyMapper.class})
public abstract class ProductResponsibleUnitMapper {
@Autowired
private LicenseMapper licenseMapper;
@Mappings({@Mapping(target = "licenses", ignore = true)})
public abstract ProductResponsibleUnit fromEntity(ProductResponsibleUnitEntity entity, @Context Boolean startedFromPru);
@Mappings({@Mapping(target = "licenses", ignore = true)})
public abstract ProductResponsibleUnitEntity toEntity(ProductResponsibleUnit domain, @Context Boolean startedFromPru);
@AfterMapping
protected void mapLicenseEntities(ProductResponsibleUnit pru, @MappingTarget ProductResponsibleUnitEntity pruEntity, @Context Boolean startedFromPru){
if(startedFromPru) {
pruEntity.getLicenses().clear(); //probably not necessary
pru.getLicenses().stream().map(license -> licenseMapper.toEntity(license, startedFromPru)).forEach(pruEntity::addLicense);
}
}
@AfterMapping
protected void mapLicenseEntities(ProductResponsibleUnitEntity pruEntity, @MappingTarget ProductResponsibleUnit pru, @Context Boolean startedFromPru){
if(startedFromPru) {
pru.getLicenses().clear(); //probably not necessary
pruEntity.getLicenses().stream().map(licenseEntity -> licenseMapper.fromEntity(licenseEntity, startedFromPru)).forEach(pru::addLicense);
}
}
}
Mapper for child:
@Mapper(componentModel = "spring", uses = { DateTimeMapper.class, CompanyCodeMapper.class, ProductHierarchyMapper.class,
CountryCodeMapper.class })
public abstract class LicenseMapper {
@Autowired
private ProductResponsibleUnitMapper pruMapper;
@Mappings({ @Mapping(source = "licenseeId", target = "licensee"), @Mapping(target = "licensor", ignore = true) })
public abstract License fromEntity(LicenseEntity entity, @Context Boolean startedFromPru);
@Mappings({ @Mapping(source = "licensee", target = "licenseeId"), @Mapping(target = "licensor", ignore = true) })
public abstract LicenseEntity toEntity(License domain, @Context Boolean startedFromPru);
@AfterMapping
protected void mapPru(LicenseEntity licenseEntity, @MappingTarget License license,
@Context Boolean startedFromPru) {
//only call ProductResponsibleUnitMapper if mapping did not start from PRU -> startedFromPru is false
if (!startedFromPru) {
license.setLicensor(pruMapper.fromEntity(licenseEntity.getLicensor(), startedFromPru));
}
}
@AfterMapping
protected void mapPru(License license, @MappingTarget LicenseEntity licenseEntity,
@Context Boolean startedFromPru) {
//only call ProductResponsibleUnitMapper if mapping did not start from PRU -> startedFromPru is false
if (!startedFromPru) {
licenseEntity.setLicensor(pruMapper.toEntity(license.getLicensor(), startedFromPru));
}
}