Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapstruct: how to qualify an IterableMapping function

Tags:

java

mapstruct

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 ?

like image 759
Philippe Gonday Avatar asked Jun 21 '26 02:06

Philippe Gonday


1 Answers

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

like image 150
Philippe Gonday Avatar answered Jun 23 '26 15:06

Philippe Gonday



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!