Java 同一类型的多个字段的从字符串到嵌套对象的Mapstruct

Java 同一类型的多个字段的从字符串到嵌套对象的Mapstruct,java,mapstruct,Java,Mapstruct,我有一个带有字段的实体类: 客户发送方 客户收件人 我有DTO类和字段: 长senderId 长受体 如果我喜欢这个: @Mappings({ @Mapping(source = "senderId", target = "sender.id"), @Mapping(source = "recipientId", target = "recipient.id") }) public Entity toEntity(DTO) { //... entity.setS

我有一个带有字段的实体类:

  • 客户发送方
  • 客户收件人 我有DTO类和字段:

  • 长senderId
  • 长受体 如果我喜欢这个:

    @Mappings({ @Mapping(source = "senderId", target = "sender.id"), @Mapping(source = "recipientId", target = "recipient.id") })
    
    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将生成如下代码:

    @Mappings({ @Mapping(source = "senderId", target = "sender.id"), @Mapping(source = "recipientId", target = "recipient.id") })
    
    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使用发件人和收件人的收件人id而不是收件人id来创建客户端收件人,使用发件人id来创建客户端发件人

    因此,我发现更好的方法是使用我认为不那么优雅的表达方式:

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

    您能建议我如何映射它们吗?

    在错误解决之前,您需要定义方法并使用
    qualifiedby
    qualifiedByName
    。有关更多信息,请参阅文档

    您的映射器应该如下所示:

    @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);
    }
    

    在解决错误之前,您需要定义方法并使用
    qualifiedby
    qualifiedByName
    。有关更多信息,请参阅文档

    您的映射器应该如下所示:

    @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);
    }
    

    这是一个错误。我已经为此创建了一个bug。我已经为此创造了