Rewriting collection mapping from orika to mapstruct

880 views Asked by At

Could you tell me how to rewrite mapping from orika to mapstruct using @Mapping.

factory.classMap(SomeEntity.class, SomeDto.class)
         .fieldAToB("items{innerItem.id}", "innerItemIds{}")
         .byDefault().register();

Not to use additional methods.

Does this exist the way to write something like

@Mapping(source = "items{innerItem.id}", target = "innerItemIds{}")
SomeDto map(SomeEntity entity);
1

There are 1 answers

0
Filip On BEST ANSWER

I am not entirely familiar with how Orika works. However, in MapStruct you can provide custom methods for mappings that MapStruct cannot do. There is no other way without using additional methods.

In your case you'll need to do something like:

@Mapper
public interface MyMapper {


    @Mapping(target = "innerItemIds", source = "items")
    SomeDto map(SomeEntity entity);

    default String map(InnterItem item) {
        return item == null ? null : item.getId();
    }

}

You will use the @Mapping to tell MapStruct that it needs to map the items collection to the innerItemIds. I assume that items is a Collection<InnerItem> and innerItemIds is a Collection<String>.