24

I have two methods for mapping entity to domain.

RDomain entityToDomain(REntity rEntity)

/*
this method ignores some of the fields in the domain.
*/
RDomain entityToDomainLight(REntity rEntity)

I'm getting Ambiguous mapping methods found for mapping collection element when I try to define mapping method for List of entities to domains.

List<RDomain> entitiesToDomains(List<REntity> rEntities)

Is there a way to define which method to use for mapping collection of objects

saw303
  • 8,051
  • 7
  • 50
  • 90
santhosh
  • 405
  • 2
  • 6
  • 15

2 Answers2

40

As @Filip suggested, it is better to do something like that :

RDomain entityToDomain(REntity rEntity)

@Named(value = "useMe")
RDomain entityToDomainLight(REntity rEntity)

@IterableMapping(qualifiedByName = "useMe")
List<RDomain> entitiesToDomains(List<REntity> rEntities)
ihebiheb
  • 3,673
  • 3
  • 46
  • 55
0

As far as I understand Mapstruct, there is no wo to tell a mapper

List<RDomain> entitiesToDomains(List<REntity> rEntities)

which of your to mapping methods it should use. But you can implement entitiesToDomains as a Java 8 default method on your mapper interface.

default List<RDomain> entitiesToDomains(List<REntity> rEntities) {

    List<RDomain> domains = new ArrayList<>();

    for(REntity r : rEntities) {
       //delegate to your dedicated mapper
       domains.add(entityToDomainLight(r));
    }

    return domains;

}
saw303
  • 8,051
  • 7
  • 50
  • 90
  • 12
    You can actually achieve that by using [`IterableMapping#qualifiedByName`](http://mapstruct.org/documentation/stable/api/org/mapstruct/IterableMapping.html#qualifiedByName--) or [`IterableMapping#quelifiedBy`](http://mapstruct.org/documentation/stable/api/org/mapstruct/IterableMapping.html#qualifiedBy--). It is in the documentation, but not that exposed :) – Filip Mar 18 '18 at 21:10
  • Cool. I did not know that. Going to update by answer – saw303 Mar 19 '18 at 05:24
  • 2
    Of course you would need to put a qualifier (MapStruct one) on the entity mapper methods – Filip Mar 19 '18 at 06:05