Search code examples
javaspring-bootmodelmapper

How to skip properties in destination source in modelmapper?


I have two classes. RequestDTO and Entity. I want to map RequestDTO to the Entity. In that case, I want to insert one of the Entity property manually which means that property is not in the Request DTO. How to achieve this using modelmapper.

public class RequestDTO {

    private String priceType;

    private String batchType;
}

public class Entity {


    private long id;

    private String priceType;

    private String batchType;
}

Entity newEntity = modelMapper.map(requestDto, Entity.class);

But this does not work, it says it can't convert string to long. I request a solution to this or a better approach to this.


Solution

  • If you want to perform the mapping manually (ideally for dissimilar objects)

    You can check the documentation for dissimilar object mapping Property Mapping,

    You can define a property mapping by using method references to match a source getter and destination setter.

    typeMap.addMapping(Source::getFirstName, Destination::setName);
    

    The source and destination types do not need to match.

     typeMap.addMapping(Source::getAge, Destination::setAgeString);
    

    If you don't want to do the mapping field by field to avoid boilerplate code

    you can configure a skip mapper, to avoid mapping certain fields to your destination model:

    modelMapper.addMappings(mapper -> mapper.skip(Entity::setId));
    

    I've created a test for your case and the mapping works for both side without configuring anything :

    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import org.junit.Before;
    import org.junit.Test;
    import org.modelmapper.ModelMapper;
    
    import static junit.framework.TestCase.assertEquals;
    import static junit.framework.TestCase.assertNotNull;
    
    public class ModelMapperTest {
    
        private ModelMapper modelMapper;
    
        @Before
        public void beforeTest() {
            this.modelMapper = new ModelMapper();
        }
    
        @Test
        public void fromSourceToDestination() {
            Source source = new Source(1L, "Hello");
            Destination destination = modelMapper.map(source, Destination.class);
            assertNotNull(destination);
            assertEquals("Hello", destination.getName());
        }
    
        @Test
        public void fromDestinationToSource() {
            Destination destination = new Destination("olleH");
            Source source = modelMapper.map(destination, Source.class);
            assertNotNull(source);
            assertEquals("olleH", destination.getName());
        }
    }
    
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    class Source {
        private Long id;
        private String name;
    }
    
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    class Destination {
        private String name;
    }