我有以下映射
@Mappings({ @Mapping(source = "id", target = "id"), @Mapping(source = "childId", target = "child.id") }) Entity objectDtoToEntity(ObjectDTO objectDTO);
我如何配置我的Mapper或Mappings,以便当childId为null时,将目标Entity.child设置为null而不是Entity.child.id?
对于我来说,找到的不是一个非常优雅的解决方案,而是可行的解决方案。
首先将接口更改为抽象类,然后添加 @AfterMapping
@AfterMapping
@Mappings({ @Mapping(source = "id", target = "id"), @Mapping(source = "childId", target = "child.id") }) public abstract Entity objectDtoToEntity(ObjectDTO objectDTO); @AfterMapping public Entity doAfterMapping(@MappingTarget Entity entity) { if (entity != null && entity.getChild().getId() == null) { entity.setChild(null); } return entity; }