简体   繁体   中英

Rewriting collection mapping from orika to mapstruct

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);

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> .

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