Skip to content

Consider to use addAll for mapping from Iterabale. #320

@AndreiPetrooshin

Description

@AndreiPetrooshin

Hi, I have the following situation:
I need to map Entity object from Customer object (protos).

@Mapping(target = "document", qualifiedByName = "mapToDocuments", source = "customer")
Entity mapToEntity(Customer customer);
    @Named("mapToDocuments")
    public List<Document> mapToDocuments(Customer customer) {
        List<Document> documents = new ArrayList<>();
        documents.add(mapToDocument1(customer.getDocument()));
        documents.add(mapToDocument2(customer.phone()));
        return documents;
    }

The main problem, that I have to create 2 Documents from 2 different fields of Customer objects.

But this approach doesn't work since mapper looking for method with return type Document instead of List.

Currently using CollectionMappingStrategy.ADDER_PREFERRED we can map only from (Customer.List<Document> or Document) to Entity.List<Document> in both cases entity.addDocument(Document document) will be used.

Can we tell somehow mapstruct to use addAll when we specify qualifiedByName which returns List<Docuement>?

Or maybe you can suggest other ways to get around this problem?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions