25

I would like to map between UserDTO and User, but excluding one field, say city. How can I do that, cause I though that this approach would work, but it doesn't:

ModelMapper modelMapper = new ModelMapper();

modelMapper.typeMap(UserDTO.class,User.class).addMappings(mp -> {
    mp.skip(User::setCity);
});
user3529850
  • 1,632
  • 5
  • 32
  • 51

3 Answers3

21

Because of the generic parameters, we couldn't use the lambda expression.

ModelMapper modelMapper = new ModelMapper();
modelMapper.addMappings(new PropertyMap<Dto, Source>() {
                @Override
                protected void configure() {
                    skip(destination.getBlessedField());
                }
            });
Muhammed Ozdogan
  • 5,341
  • 8
  • 32
  • 53
7

For the configuration to work need to add:

modelMapper.getConfiguration().setAmbiguityIgnored(true);

E.g.

ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration().setAmbiguityIgnored(true);
modelMapper.addMappings(clientPropertyMap);
modelMapper.map(UserDTO, User);


PropertyMap<UserDTO, User> clientPropertyMap = new PropertyMap<UserDTO, User>() {
    @Override
    protected void configure() {
        skip(destination.getCity());
    }
};
sɐunıɔןɐqɐp
  • 3,332
  • 15
  • 36
  • 40
Sergiu Ionita
  • 71
  • 1
  • 4
1

For the configuration to work need to add:
modelMapper.getConfiguration().setAmbiguityIgnored(true);

This is true only when the destination field matches to multiple source fields. Skipping the setting of a destination field will work without the above if there is either a 1-1 or a 0-1 match between source-destination.

TheAppFoundry
  • 93
  • 1
  • 9