Код: Выделить всё
@Data
public class Parent {
long id;
String name;
}
@Data
public class Child extends Parent {
String nickName;
}
@Data
public class Grandchild extends Child {
int count;
}
Код: Выделить всё
@Data
public class ParentDto {
long idDto;
String name;
}
@Data
public class ChildDto extends ParentDto {
String nickNameDto;
}
@Data
public class GrandchildDto extends ChildDto {
int countDto;
}
Код: Выделить всё
@MapperConfig(mappingInheritanceStrategy = MappingInheritanceStrategy.AUTO_INHERIT_FROM_CONFIG,
unmappedSourcePolicy = ReportingPolicy.ERROR,
unmappedTargetPolicy = ReportingPolicy.ERROR)
public interface ParentMapperConfig {
@Mapping(target = "idDto", source = "id")
ParentDto map(Parent parent);
@Mapping(target = "id", source = "idDto)
Parent map(ParentDto parentDto);
}
Код: Выделить всё
@Mapper(config = ParentMapperConfig.class)
public interface ParentToParentDto {
ParentDto map(Parent parent);
Parent map(ParentDto parentDto);
}
Когда я создаю преобразователь ChildToChildDto, я бы предпочел не повторять сопоставления ididDto, но я не могу понять, как это сделать. Я пробовал:
Код: Выделить всё
@MapperConfig(uses = ParentMapperConfig.class)
public interface ChildMapperConfig {
@Mapping(target = "nickNameDto", source = "nickName")
ChildDto map(Child child);
@Mapping(target = "nickName", source = "nickNameDto)
Child map(ChildDto childDto);
}
Код: Выделить всё
@Mapper(config = ChildMapperConfig.class)
public interface ChildToChildDto {
ChildDto map(Child child);
Child map(ChildDto childDto);
}
К сожалению, ChildToChildDto не получает сопоставление ididDto от родителя. Кажется, это должна быть решенная проблема, но я не могу найти решение.
Подробнее здесь: https://stackoverflow.com/questions/798 ... ield-names
Мобильная версия