Does modelMapped allow collection mapping?
Eg:
public class User{
string name;
List<Role> roles = new ArrayList<Role>;
......
}
public class Role{
string name;
string description
}
I would like to map dataobject USER to DTO USER. How do I map
user.roles data object to DTO's user.roles.
Have you found any support for this currently.
If not I would be interested in implementing it
modelMapper.addMappings(new PropertyMap<Configuration, ConfigurationDTO>() {
@Override
protected void configure() {
Converter<List<Location>, List<Long>> locationConverter = new Converter<List<Location>, List<Long>>() {
public List<Long> convert(MappingContext<List<Location>, List<Long>> context) {
List<Long> result = new ArrayList<Long>();
for (Location loc : context.getSource())
result.add(loc.id);
return result;
}
};
using(locationConverter).map(source.getLocations()).setLocationIds(null);
}
});
There's two things we need to accomplish. The first is making source that Confirmation.locations maps to configurationDTO.locationIds. We can do that by creating a mapping inside a PropertyMap:
map(source.getLocations()).setLocationIds(null);
But of course this will fail since getLocations returns a different type then what setLocationIds is expecting. So we include a converter that converts between those two types.
Cheers,
Jonathan
modelMapper.addConverter(new Converter<Location, Long>() {
public Long convert(MappingContext<Location, Long> context) {
return context.getSource().id;
}
});