I have the following classes:
public class MyEntity {
private Set<MyOtherEntity> other;
}
public class MyDTO {
private List<MyOtherDTO> other;
}
I created two PropertyMaps
(using ModelMapper
), one for each conversion from and to DTO
public class DTOToEntityPropertyMap extends PropertyMap<MyDTO, MyEntity> {
@Override
protected void configure() {
List<MyOtherDTO> myOtherDTOs = source.getOther();
Set<MyOtherEntity> myOtherEntities = new HashSet<>();
for (MyOtherDTO myOtherDTO : myOtherDTOs) {
MyOtherEntity myOtherEntity = ModelMapperConverterService.convert(myOtherDTO, MyOtherEntity.class);
myOtherEntities.add(myOtherEntity);
}
map().setOther(myOtherEntities);
}
}
public class EntityToDTOPropertyMap extends PropertyMap<MyEntity, MyDTO> {
@Override
protected void configure() {
Set<MyOtherEntity> myOtherEntities = source.getOther();
List<MyOtherDTO> myOtherDTOs = new ArrayList<>();
for (MyOtherEntity myOtherEntity : myOtherEntities) {
MyOtherDTO myOtherDTO = ModelMapperConverterService.convert(myOtherEntity, MyOtherDTO.class);
myOtherDTOs.add(myOtherDTO);
}
map().setOther(myOtherDTOs);
}
}
Adding the PropertyMaps
to the ModelMapper
creates the following error:
Caused by: org.modelmapper.ConfigurationException: ModelMapper configuration errors:
1) Invalid source method java.util.List.add(). Ensure that method has zero parameters and does not return void.
I guess I cannot use List.add()
in the configuration of a PropertyMap
.
Then, what is the best way to implement the conversion of a List to a Set and backwards in the ModelMapper
?