简体   繁体   中英

MapStruct - Mapping two lists with different object types to a third list with another object type

I'm having a problem when trying to map two lists into another list using mapstruct. The structure is as follows:

public class classA{
    List<ObjectA> list;
}

public class classB{
    List<ObjectB> list;
}

public class classC{
    List<ObjectC> list;
}

I need to map both lists from classC and classB to the list in classA (mapping from objectB and objectC to objectA is already done). I tried the following:

@Mappings({
    @Mapping(target="list", source="obj1.list"),
    @Mapping(target="list", source="obj2.list")
})
classA map(classB obj1, classC obj2);

//the other mappers from objectC and objectB to objectA

This gives me compile errors. Can someone help me mapping this lists? I heard something about an annotation parameter called exception, but couldn't solve my problem with it.

Thanks!

This cannot be done by mapstruct. There's always business logic involved in merging 2 list. So you need b to write your own method implementation. I recently wrote a FAQ item on the MapStruct web page. This applies to a pre existing list @MappingTarget. But similar reasoning applies to merging 2 source lists into one target. http://mapstruct.org/faq/#why-is-it-not-possible-for-mapstruct-to-generate-implementations-for-iterable-stream-and-map-types-from-update-mappingtarget-methods

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM