17
votes

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

2

2 Answers

23
votes

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)
0
votes

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;

}