简体   繁体   English

从具有相同类型的多个字段的字符串构造到嵌套对象

[英]Mapstruct from string to nested object for multiple fields with the same type

I have Entity class with fields: 我有Entity类的字段:

  1. Client sender; 客户发件人;
  2. Client recipient; 客户收件人;

I have DTO class with fields: 我有DTO课的字段:

  1. long senderId; long senderId;
  2. long recipientId; long recipientId;

If I do like this: 如果我喜欢这样:

@Mappings({ @Mapping(source = "senderId", target = "sender.id"), @Mapping(source = "recipientId", target = "recipient.id") })

Mapstruct will generate code like this: Mapstruct将生成如下代码:

public Entity toEntity(DTO) {
        //...
        entity.setSender( dtoToClient( dto ) );
        entity.setRecipient( dtoToClient( dto ) );
        //...

    protected Client dtoToClient(Dto dto) {
        Client client = new Client();
        client.setId( dto.getRecipientId() ); // mapstruct takes recipient id for sender and recipient
        return client;
    }
}

Mapstruct takes recipient id for sender and recipient instead of recipient id to create Client recipient and sender id to create Client sender. Mapstruct获取发件人和收件人的收件人ID而不是收件人ID,以创建客户端收件人和发件人ID以创建客户端发件人。

So the better way I've found is using expression that is not so elegant as far as I can see: 所以我发现更好的方法是使用不那么优雅的表达式,据我所知:

@Mappings({
      @Mapping(target = "sender", expression = "java(createClientById(dto.getSenderId()))"),
      @Mapping(target = "recipient", expression = "java(createClientById(dto.getRecipientId()))")
})

Could you plz suggest me how to map them? 你能告诉我如何映射它们吗?

Until the bug is resolved you will need to define the methods and use qualifedBy or qualifiedByName . 在解决错误之前,您需要定义方法并使用qualifedByqualifiedByName More info about there here in the documentation. 有关此处的更多信息,请参阅文档。

Your mapper should look like: 您的映射器应如下所示:

@Mapper
public interface MyMapper {

    @Mappings({
        @Mapping(source = "dto", target = "sender", qualifiedByName = "sender"),
        @Mapping(source = "dto", target = "recipient", qualifiedByName = "recipient")
    })
    Entity toEntity(Dto dto);


    @Named("sender")
    @Mapping(source = "senderId", target = "id")
    Client toClient(Dto dto);

    @Named("recipient")
    @Mapping(source = "recipientId", target = "id")
    Client toClientRecipient(Dto dto);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Mapstruct将多个源字段映射到相同类型的目标字段 - Map multiple source fields to same type target fields with Mapstruct Mapstruct-忽略嵌套集合中的字段-克隆对象时不起作用 - Mapstruct - ignore fields from nested collection - not working when cloning the object MapStruct:是否可以为类型的所有嵌套字段指定使用相同命名的映射? - MapStruct: Is it possible to specify using the same named mapping for all nested fields of a type? Map 嵌套字段与 MapStruct - Map nested fields with MapStruct mapstruct:如何从mapstruct中的对象属性构造字符串? - mapstruct: How to construct a string from an object's property in mapstruct? Mapstruct:当源为 Null 时如何将目标字符串默认为空字符串(两个字段具有相同的名称和类型)Java / Spring - Mapstruct: How to default a target String to Empty String when the Source is Null (Both fields have the same name and type) Java / Spring MapStruct:来自不同实体的字段映射到相同实体的不同字段 - MapStruct: Fields from Different Entities mapping to Same Entity Different fields Java-对象中具有相同数据类型的多个字段 - Java - Multiple fields of same data type in an object 使用mapstruct映射嵌套对象 - Mapping nested object with mapstruct 来自实体映射结构中的多个字符串的 Dto - Dto from multiple string in Entity mapstruct
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM