简体   繁体   English

如何在嵌套 Mapstruct 映射器中的模糊映射方法中指定 select?

[英]How to specify select among ambiguous mapping methods in nested Mapstruct mappers?

We have a particular case in which a Mapstruct mapper can choose between 2 different alternative methods from its dependency (namely ProductionPlanDetailAutoMapper), but, unable to do so, returns a compile error SupplyPlanProjectionAutoMapper.java Can't map map value "java.util.List<ProductionPlanDetail>" to "java.util.List<ProductionPlanDetailDTO> ". Consider to declare/implement a mapping method: "java.util.List<ProductionPlanDetailDTO> map(java.util.List<ProductionPlanDetail> value)". We have a particular case in which a Mapstruct mapper can choose between 2 different alternative methods from its dependency (namely ProductionPlanDetailAutoMapper), but, unable to do so, returns a compile error SupplyPlanProjectionAutoMapper.java Can't map map value "java.util.List<ProductionPlanDetail>" to "java.util.List<ProductionPlanDetailDTO> ". Consider to declare/implement a mapping method: "java.util.List<ProductionPlanDetailDTO> map(java.util.List<ProductionPlanDetail> value)". SupplyPlanProjectionAutoMapper.java Can't map map value "java.util.List<ProductionPlanDetail>" to "java.util.List<ProductionPlanDetailDTO> ". Consider to declare/implement a mapping method: "java.util.List<ProductionPlanDetailDTO> map(java.util.List<ProductionPlanDetail> value)".

The source code is composed of 2 mappers: an overview is presented as well as their respective codes:源代码由 2 个映射器组成:概述以及它们各自的代码:

结构图

Mapper 1:映射器 1:

@Mapper(componentModel = "spring", uses={ProductionPlanDetailAutoMapper.class}) 
public interface SupplyPlanProjectionAutoMapper {

    @Mapping(source = "material.id", target = "materialId")
    public PlanningProjectionDTO convert(PlanningProjection);
    
    public Map<PlanningProjectionDTO, List<ProductionPlanDetailDTO>> convertMapProductionPlanDetail (Map<PlanningProjection, List<ProductionPlanDetail>> map);
}

Mapper 2:映射器 2:

@Mapper(componentModel = "spring", uses={RecipeAutoMapper.class})
public interface ProductionPlanDetailAutoMapper {
    
    @Mapping(source = "location.id", target = "locationId")
    @Mapping(source = "receitaProducaoOperacao", target = "productionRecipeOperation")
    @Mapping(source = "dataReferencia", target = "referenceDate")
    @Named(value = "convertWithRecipeDetail")
    public ProductionPlanDetailDTO convertWithRecipeDetail(ProductionPlanDetail);
                
    @Mapping(source = "receitaProducaoOperacao.listaTecnica.materialOutput.id", target = "outputMaterialId")
    @Mapping(source = "receitaProducaoOperacao.listaTecnica.id", target = "billOfMaterialsId")
    @Mapping(source = "receitaProducaoOperacao.receitaProducaoOperacaoCompositeKey.receitaProducao.id", target = "productionRecipeId")
    @Mapping(source = "receitaProducaoOperacao.recursoProdutivo.id", target = "productionResourceId")
    @Mapping(source = "receitaProducaoOperacao.receitaProducaoOperacaoCompositeKey.posicaoOperacao", target = "productionRecipeOperationSequence")
    @Mapping(source = "location.id", target = "locationId")
    @Mapping(source = "dataReferencia", target = "referenceDate")
    @Named(value = "convertWithoutRecipeDetail") 
    public ProductionPlanDetailDTO convertWithoutRecipeDetail(ProductionPlanDetail productionPlanDetail);

    @Named(value = "convertToListWithRecipeDetail") 
    @IterableMapping(qualifiedByName = "convertWithRecipeDetail") 
    public List<ProductionPlanDetailDTO> convertToDTOListWithRecipeDetail (List<ProductionPlanDetail> productionPlanDetailList);
    
    @Named(value = "convertToListWithoutRecipeDetail") 
    @IterableMapping(qualifiedByName = "convertWithoutRecipeDetail") 
    public List<ProductionPlanDetailDTO> convertToDTOListWithoutRecipeDetail (List<ProductionPlanDetail> productionPlanDetailList);
}

Ambiguities between methods inside the second mapper are resolved by the conjunction of @Name and @IterableMapping#QualifiedByName第二个映射器内部方法之间的歧义通过@Name 和@IterableMapping#QualifiedByName 的结合来解决

We have tried the same method by attaching the below annotation to the convertMapProductionPlanDetail method, to no avail:我们通过将以下注释附加到 convertMapProductionPlanDetail 方法来尝试相同的方法,但无济于事:

@IterableMapping(qualifiedByName = "convertToListWithRecipeDetail")

Although not in the documentation the Mapstruct developers have already thought about it.尽管 Mapstruct 开发人员已经在文档中没有考虑过它。 While combing the documentation I've found this: valueQualifiedByName在梳理文档时,我发现了这个: valueQualifiedByName

The solution was to use MapMapping and specify which of the 2 @Name methods should be used to populate the Values section (where List(ProductionPlanDetail) resides) of the map:解决方案是使用 MapMapping 并指定应使用 2 个 @Name 方法中的哪一个来填充 map 的 Values 部分(List(ProductionPlanDetail) 所在的位置):

@MapMapping(valueQualifiedByName = "convertToDTOListWithRecipeDetail")
public Map<PlanningProjectionDTO, List<ProductionPlanDetailDTO>> convertMapProductionPlanDetail (Map<PlanningProjection, List<ProductionPlanDetail>> map);

Which solves the problem quite elegantly.这非常优雅地解决了这个问题。 On the other side, if the conversion issue resided in the map key, @MapMapping#keyQualifiedByName should be used instead.另一方面,如果转换问题存在于 map 密钥中,则应改用 @MapMapping#keyQualifiedByName。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM