简体   繁体   中英

ModelMapper: Mix implicit and explicit mapping?

In order to write the minimum amount of code required, I'm trying to let ModelMapper generate its implicit mapping and only write explicit property mappings for those properties that it couldn't auto-map.

If I let ModelMapper generate an implicit mapping using:

modelMapper.createTypeMap(SourceType.class, DestType.class);

it complains about setSomeId having multiple possible mappings. I then tried to fix just that using:

modelMapper.addMappings(new PropertyMap<SourceType, DestType>() {
    protected void configure() {
        map().setSomeId(source.getProperty().getWeirdID());
    }
});

However, I found that ModelMapper still complains, because an exception is actually thrown on createTypeMap , so it doesn't have a chance to reach my custom mapping code.

If I invert both statements, I get an error:

java.lang.IllegalStateException: A TypeMap already exists for class SourceType and class DestType

If I leave out createTypeMap completely, ModelMapper complains about missing mappings for all other properties of the DestType (those who it was able to map automatically with createTypeMap ).

I found no explicit clue in the Documentation whether mixing implicit with explicit mappings is supported and how it's done.

Can anyone help?

Instead of ModelMapper.createTypeMap try ModelMapper.addMappings (first). This still creates (and returns) a TypeMap , but takes your PropertyMap into account while doing so.

Don't know if this is still an issue to you, and I must admit I am new to ModelMapper. Never the less I have struggled with the same issue you have/had. I seem to have solved it.

    TypeMap<RateDTO, Rate> rateDTORateTypeMap = modelMapper.getTypeMap(RateDTO.class, Rate.class);
    if(rateDTORateTypeMap == null) {
        rateDTORateTypeMap = modelMapper.createTypeMap(RateDTO.class, Rate.class);
    }
    rateDTORateTypeMap.setProvider(request -> {
        RateDTO source = RateDTO.class.cast(request.getSource());
        CurrencyAndAmount price = new CurrencyAndAmount(source.getPrice().getCurrencyCode(), source.getPrice().getAmount());
        return new Rate(price, source.getPaymentDate(), source.getPaymentId());
    });

Basically I first try and get the TypeMap if its already there, otherwise I create a new/modified mapping.

Hope it helps

Let's say we want to map Source source to Destination destination . Instead of

Destination destination = modelMapper.map(source, Destination.class);

In order to combine explicit and implicit mappings we nedd to add implicitMappings() in TypeMap

public Destination mapSourceToDestination(Source source) {
            TypeMap<Source, Destination> typeMap = modelMapper
                    .typeMap(Source.class, Destination.class)
                    .implicitMappings()
                    .addMappings(mapper -> {
                        mapper.map(source -> source.getPropetryOne().getSubPropetryOne(), Destination::setPropetryA);
                    });
            return typeMap.map(source);
        }

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