I am having trouble wrapping my head around subclass mappings. I have 3 classes:
@Data public class Parent { long id; String name; public boolean isEmpty() { return StringUtils.isEmpty(name); } @Data public class Child extends Parent { String nickName; } @Data public class Grandchild extends Child { int count; } and 3 corresponding DTO classes:
@Data public class ParentDto { long idDto; String name; } @Data public class ChildDto extends ParentDto { String nickNameDto; } @Data public class GrandchildDto extends ChildDto { int countDto; } I am trying to put together the mappers but having a hard time getting the mappings to work. I have tried using subclassMappings as follows:
@Mapper(subclassExhaustiveStrategy = SubclassExhaustiveStrategy.RUNTIME_EXCEPTION) public interface ParentMapper { @SubclassMapping(target = GrandchildDto.class, source = Grandchild.class) @SubclassMapping(target = ChildDto.class, source = Child.class) @Mapping(target = "idDto", source = "id") @BeanMapping(ignoreUnmappedSourceProperties = {"empty"} ParentDto mapToParentDto(Parent parent) } @Mapper(uses = ParentMapper.class) ... not sure this is correct/necessary public interface ChildMapper { @SubclassMapping(target = GrandchildDto.class, source = Grandchild.class) @Mapping(target = "nickNameDto", source = "nickName") ChildDto mapToChildDto(Child child) } @Mapper(uses = ChildMapper.class) ... not sure this is correct/necessary public interface GrandchildMapper { @Mapping(target = "countDto", source = "count") GrandchildDto mapToGrandChildDto(Grandchild grandchild) } I have a test for the ChildMapper which fails because the id (from Parent) does not match the idDto (from ParentDto). I have also tried handling the inheritance with MapperConfigs but did not have much luck there either.
What is the best way to handle multiple inheritance with differing parameter names? Thanks