Reputation: 187
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.
Upvotes: 3
Views: 9937
Reputation: 187
I have previously used a ModelMapper
version 0.6.5
and upgrading it to the 1.1.0
issue was fixed
Upvotes: 0
Reputation: 871
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]
Upvotes: 8