I am using ModelMapper v0.7.7 My classes look like below:
public class ParentClassOne {
protected String someValue1;
protected String someValue2;
protected ChildClassOne itemOne;
public ChildClassOne getItemOne() {
return itemOne;
}
public void setItemOne(ChildClassOne itemOne) {
this.itemOne = itemOne;
}
...setter and getter methods for the rest of fields...
}
public class ParentClassOne {
...this class contains the same elements like the first one...
}
My ModelMapper configuration looks like this below:
ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STANDARD);
modelMapper.getConfiguration().setMethodAccessLevel(AccessLevel.PUBLIC);
modelMapper.getConfiguration().setFieldAccessLevel(AccessLevel.PROTECTED);
When I want to convert the one object to another one I am using following source code:
info.p2.ParentClassOne result = modelMapper.map(data, info.p2.ParentClassOne.class);
Of course it implies that data is object that was created by the first class from package info.p1 .
When I execute this source code only these "String" variables (someValue1 and someValue2) will be filled but variable "itemOne" will be null even if I pass full object that has all attributes filled.
Indication: The both classes are generated via maven with org.apache.cxf -> wsdl2java package so both of them contain special "javax.xml" annotations.
Edit1: Class ChildClassOne exists in both packages. When ModelMapper tries to convert this, it recognizes that this variable in both classes have totally different type due to different packages and ModelMapper will not map this variable.
It is the same thing with string variables (someValue1, someValue2) but String type is the same in both classes and this type exists in a same package then ModelMapper will map them.
Of course I can add some manual steps to solve this simple example but in real situation I have much more complicated object that contains many nested objects.
Edit2: The problem that I noticed, I did know when I asked the questions, are collections Lists in my ChildClassOne. ModelMapper can not map any object that contains any type of collection. In my classes list looks like below:
public ChildClassOne {
protected List<ChildAnyClass> children;
...getter method...
Notice: ...there is no setter method for the list....
}
If there is no collection in nested objects ModelMapper will map all of them but in my case I have collection in each of my nested objects.
I was searching and found following solution that can be used:
ModelMapper modelMapper = new ModelMapper();
Converter<ObjectOne, ObjectTwo> converter = new Converter<ObjectOne, ObjectTwo>() {
public ObjectTwo convert(MappingContext<ObjectOne, ObjectTwo> context) {
ObjectTwo dest = new ObjectTwo();
if (context.getSource() != null) {
// Converts list from source object into the list of destination object
Type listType = new TypeToken<List<ObjectTwo>>() {}.getType();
List<ObjectTwo> items = modelMapper.map(context.getSource().getList(), listType);
dest.getList().addAll(items);
return dest;
} else {
return dest;
}
}
};
Now it is just necessary to add your convertor into ModelMapper with following source code:
modelMapper.addConverter(converter);
This is a good solution if you have one or two nested collection but in case where you have multiple of them then you have to write for each of them convertor.
If someone knows how to solve this issue with multiple of nested collection please share with us.