Тем временем я нашел решение: сначала я игнорирую рассматриваемое поле и отображаю его в «AfterMapping». В моем случае я хочу нанести на карту и ребенка самостоятельно. В моем случае @Context
содержит только Boolean
(boolean
не работает), который сообщает, где я вошел в цикл (startedFromPru
pru - родитель):
Mapper для родителя (содержит набор лицензий):
@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 для детей:
@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));
}
}