Hi there,
when you look at the "Getting started"-guide or the UserManual, there are two ways to configure Mappings. One for Java 6/7 (using inline subclasses of PropertyMap) and one for Java 8 (using lambda expressions).
BTW: The lambda expression examples are unclear, since mm.addMapping(ExpressionMap) suggests ModelMapper.addMapping() is meant, which does not exist for ExpressionMap as a parameter.
So I thought those two approaches would produce the same result, but I found they don't.
Why are these two things not the same?
ModelMapper mm = new ModelMapper();
// New approach Java 8
mm.typeMap(A.class, ADto.class).addMappings(mapper -> {
mapper.map(A::getSomeId, ADto::setSomeId);
mapper.map(src -> src.getSomething().getOtherId(), ADto::setOtherId);
});
// Old approach Java 6/7
mm.addMappings(new PropertyMap<A, ADto>() {
@Override
protected void configure() {
map().setSomeId(source.getSomeId());
map().setOtherId(source.getSomething().getOtherId());
}
});
The Java 8 version above gives an error stating: "ADto.setOtherId() matches multiple source propertiy hierarchies" ...
The Java 6/7 version does not produce this problem.
Maybe I completely misunderstood, but then it would be great to have an explanation, what the difference between the two approaches are and when to use which approach.
Greetings,
Malte