'Updating entity using model mapper

I have a strange problem with model mapper. Probably I missunderstand how it works. What I have: an example Model class:

class Model{
 String name;
 String surname;
 Integer age;
 ...and much much more

And a method

private void foo(){
        ModelMapper modelMapper = new ModelMapper();
        Model model = Model.builder().name("foo").surname("bar").age(23).build();
        Model newModel = Model.builder().name("john").build();
        modelMapper.map(newModel, model);
        System.out.println(model.toString());
}

And the output is: Model(name=john, surname=null, age=null) But what I expect Model(name=john, surname=bar, age=23) Can I do this using model mapper? If not, how to do this easily (i dont want update manually each property)? Thanks.



Solution 1:[1]

You can do it easily using Lombok builder @Builder.Default You just need to update your Model class

public class Model {
    @Builder.Default
    String name = "foo";
    @Builder.Default
    String surname = "bar";
    @Builder.Default
    Integer age = 23;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mpdgr