Search code examples
javamapstruct

Mapstruct: how to qualify an IterableMapping function


I have a mapper between an entity and a DTO:

@Mapper(componentModel="cdi", uses = { RegionMapper.class })
public interface ClusterMapper {
    @Mapping(target="regions", ignore=true)
    ClusterDto map(Cluster entity);

    ClusterDto mapWithRegions(Cluster entity);
}

The first mapping function is a "simple mapping" to list entities, the second one is for a detailed view. I would like to have the List equivalent, so I have an annotation:

@Qualifier
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Simple {
}

And add this qualifier to the first mapping function, and to the List function:

@Mapper(componentModel="cdi", uses={RegionMapper.class})
public interface ClusterMapper {
    @Simple // <====
    @Mapping(target="regions", ignore=true)     
    ClusterDto map(Cluster entity);

    ClusterDto mapWithRegions(Cluster entity);

    @IterableMapping(qualifiedBy = Simple.class) // <====
    List<ClusterDto> map(List<Cluster> entities);

}

but even with the @Simple annotation, I have an error message:

Ambiguous mapping methods found for mapping collection element to...

How can I made the List map(List) function "choose" the first mapping function ?


Solution

  • Ok I made a mistake: for my @Simple annotation, I have imported javax.inject.Qualifier instead of org.mapstruct.Qualifier.