当前位置: 首页 > 知识库问答 >
问题:

MapStruct无法映射需要外部变量的嵌套对象

李昊苍
2023-03-14

Im试图使用MapStruct映射具有嵌套对象并需要外部变量的对象。

源->target映射是有损的,并且需要外部字符串


//Entities
public class Repro {

    @Value
    @Builder
    public static class Nested {
        @NonNull
        private String id;
        @Nullable
        private String version;
        @NonNull
        private String externalId;
    }

    @Value
    @Builder
    public static class SourceEntity {
        @NonNull
        private String id;
        @NonNull
        private String anotherId;
    }

    @Value
    @Builder
    public static class TargetEntity {
        @NonNull
        private Nested nested;
        @NonNull
        private String anotherId;
    }
}
//Mapper

@Mapper
public interface ReproMapper {

    @Mapping(target = "nested.version", ignore = true)
    @Mapping(source = "source.id", target = "nested.id")
    @Mapping(source = "source.anotherId", target = "anotherId")
    @Mapping(source = "externalId", target = "nested.externalId")
    Repro.TargetEntity fromSource(Repro.SourceEntity source, String externalId);

    @Mapping(source = "nested.id", target = "id")
    @Mapping(source = "anotherId", target = "anotherId")
    Repro.SourceEntity fromTarget(Repro.TargetEntity target);
}

Can't map property "Repro.SourceEntity source" to "Repro.Nested nested". Consider to declare/implement a mapping method: "Repro.Nested map(Repro.SourceEntity value)

它告诉我要实现一个不可行的映射方法(因为它将构造一个部分的嵌套的对象),该对象将在build()调用期间失败。

有没有办法使用MapStruct来解决这个问题,或者我只是实现我自己的映射器?

共有1个答案

孟花蜂
2023-03-14

您可以这样做(手写方法icm@mappingcontext传递ExternalID:

@Mapper
public interface ReproMapper {

    @Mapping(target = "nested",source = "source")
    @Mapping(target = "anotherId",source = "source.anotherId")
    Repro.TargetEntity fromSource(Repro.SourceEntity source, @Context String externalId);

    //A default impl that delegates to MapStruct generated method
    default Repro.TargetEntity.Nested resolveNested(Repro.SourceEntity source, @Context String externalId) {
        return delegate(source, externalId);
    }

    //Actual Mapping method, generated by MapStruct
    //Note here externalId is not @Context annotated
    @Mapping(target = "version", ignore = true)
    @Mapping(target = "id", source = "source.id")
    @Mapping(target = "externalId", source = "externalId")
    Repro.TargetEntity.Nested delegate(Repro.SourceEntity source, String externalId)

    @Mapping(source = "nested.id", target = "id")
    @Mapping(source = "anotherId", target = "anotherId")
    Repro.SourceEntity fromTarget(Repro.TargetEntity target);
}
 类似资料:
  • 我创建映射如下所示。如何将平面dto对象属性(街道、城市等)映射到域对象中的嵌套地址。当我试着去做的时候,我发现了一个错误: [错误]诊断:返回类型中的属性“Address.PostalCode”未知。@Mapping(来源=“City”,目标=“Address.City”), 还有类...

  • 我尝试使用MapStruct编写映射器类,如下所示: 目前它显示了“未知属性”“customer.customerid”和“usertypes.usertype.userid”等错误。有人能帮我用MapStruct映射所有这些元素吗? 问题2:我们如何绘制跟踪图?1)customerId usertypes->user->userid 2)pdtPrice offers->OffersType->

  • 有人能帮忙填写上面的评论部分吗?或者是否有其他选项来映射这些对象? 编辑:我尝试了下面的解决方案,但是接口实现类本身发生了变化。

  • 我使用MapStruct来映射我的实体,我使用Mockito来嘲笑我的对象。 我想用MapStruct测试一个包含映射的方法。问题是嵌套映射器在我的单元测试中总是为null(在应用程序中工作得很好)

  • 假设我有这些实体: null

  • 我如何在下面的场景中使用Mapstruct进行bean映射。 现在我想把sourceId映射到targetId,courseName映射到subjectName,studentName映射到memberName(list到list)。