MapStruct:对象的映射列表,当对象由两个对象映射时

问题描述 投票:0回答:4

假设我有这样的映射:

@Mapping(source = "parentId", target = "parent.id")
Child map(ChildDto dto, Parent parent);

现在我需要将 ChildD 的 List 映射到 Child 的列表,但它们都有相同的父级。我希望做这样的事情:

List<Child> map(List<ChildDto> dtoList, Parent parent);

但它不起作用。 有机会做吗?

java mapping mapstruct
4个回答
7
投票

我找到了如何用装饰器来实现它,谢谢@Gunnar 这是一个实现:

豆类

public class Child {
    int id;
    String name;
}
public class Parent {
    int id;
    String name;
}
public class ChildDto {
    int id;
    String name;
    int parentId;
    String parentName;
}
// getters/settes ommited

映射器

@Mapper
@DecoratedWith(ChildMapperDecorator.class)
public abstract class ChildMapper {
    public static final ChildMapper INSTANCE = Mappers.getMapper(ChildMapper.class);

    @Mappings({
            @Mapping(target = "parentId", ignore = true),
            @Mapping(target = "parentName", ignore = true)
    })
    @Named("toDto")
    abstract ChildDto map(Child child);

    @Mappings({
            @Mapping(target = "id", ignore = true),
            @Mapping(target = "name", ignore = true),
            @Mapping(target = "parentId", source = "id"),
            @Mapping(target = "parentName", source = "name")
    })
    abstract ChildDto map(@MappingTarget ChildDto dto, Parent parent);

    @IterableMapping(qualifiedByName = "toDto") // won't work without it
    abstract List<ChildDto> map(List<Child> children);

    List<ChildDto> map(List<Child> children, Parent parent) {
        throw new UnsupportedOperationException("Not implemented");
    }
}

装饰器

public abstract class ChildMapperDecorator extends ChildMapper {
    private final ChildMapper delegate;

    protected ChildMapperDecorator(ChildMapper delegate) {
        this.delegate = delegate;
    }

    @Override
    public List<ChildDto> map(List<Child> children, Parent parent) {
        List<ChildDto> dtoList = delegate.map(children);
        for (ChildDto childDto : dtoList) {
            delegate.map(childDto, parent);
        }
        return dtoList;
    }
}

对于映射器,我使用

abstract class
,而不是
interface
,因为在
interface
的情况下,您无法排除生成方法
map(List<Child> children, Parent parent)
,并且生成的代码在编译时无效。


3
投票

我按照 Gunnar 的建议使用了

@AfterMapping

@AfterMapping
public void afterDtoToEntity(final QuestionnaireDTO dto, @MappingTarget final Questionnaire entity) {
  entity.getQuestions().stream().forEach(question -> question.setQuestionnaire(entity));
}

这确保了所有问题都链接到同一问卷实体。这是解决方案的最后一部分,用于避免在创建带有子级列表的新父实体时出现 JPA 错误

save the transient instance before flushing


2
投票

就目前情况而言,这是不可能的。您可以使用装饰器或后映射方法将父对象设置为所有子对象。


0
投票

也许简单的实施比建议的解决方案更好?

List<Child> map(List<ChildDto> dtoList, final Parent parent) {
    if (dtoList == null) return null;
    return dtoList
        .stream()
        .map(x -> map(x, parent))
        .collect(Collectors.toList());
}
© www.soinside.com 2019 - 2024. All rights reserved.