3

I am planning to write a method which use to update a MyObject object with not null fields of another MyObject object.

private void updateMyObject(MyObject sourceObject, MyObject destinationObject) {
    ModelMapper mapper = new ModelMapper();
    mapper.getConfiguration().setPropertyCondition(Conditions.isNotNull());
    mapper.map(sourceObject, destinationObject);
}

public class MyObject {
    long id;

    long durationInMilliSecounds;

    //...getters and setters 
}

In here destinationObject is not getting updated. Can anybody suggest the issue of this code.

Akila
  • 187
  • 2
  • 9

2 Answers2

7

You seem to be missing some code. Perhaps the error is in that code. I am guessing that your model does not have both getters and setters, which is required by ModelMapper.

The following code works as expected:

public class modelMapperTest {
    public static void main(String[] args) {
    ModelMapper mapper = new ModelMapper();
    mapper.getConfiguration().setPropertyCondition(Conditions.isNotNull());

    MyModel sor = new MyModel(null, 5);
    MyModel des = new MyModel("yyy", 0);        

    System.out.println(des);
    mapper.map(sor, des);
    System.out.println(des);
}

with

public class MyModel {
    private String s;
    private int i;

public String getS() {
    return s;
}

public void setS(String s) {
    this.s = s;
}

public int getI() {
    return i;
}

public void setI(int i) {
    this.i = i;
}

public MyModel(String s, int i) {
    super();
    this.s = s;
    this.i = i;
}

@Override
public String toString() {
    return "MyModel [s=" + s + ", i=" + i + "]";
}

}

Prints:

MyModel [s=yyy, i=0]

MyModel [s=yyy, i=5]

Jan Larsen
  • 831
  • 6
  • 13
  • MyObject class has the getters and setters. I have update the question with that information – Akila Mar 08 '18 at 04:35
  • I don't see that the update. I am using ver. 1.1.0 are you? Please show the rest of the code and config. – Jan Larsen Mar 08 '18 at 07:12
  • I know this is old but do you know why if one of my objects has another object with a list of strings as a field with this [A, B, C] and if I try to merge it with another with that value as [A, C] I am getting [A, C, C] when I should get [A, C] – Wrong Jun 26 '19 at 09:26
0

I have previously used a ModelMapper version 0.6.5 and upgrading it to the 1.1.0 issue was fixed

Akila
  • 187
  • 2
  • 9