Having something like:
@Getter @Setter
public static class Entity {
private int hash;
private LocalDateTime createdTime;
}
and
@Getter @Setter
public static class DTO {
private String hash;
private String createdTime;
}
I need birectional mapping so I should be able to map Entity -> DTO -> Entity. In this example the property type happens to be LocalDateTime
but could be any type that needs parsing from String
or so (just to say that I am not after better way to map LocalDateTime
but in general).
There are no problems in mapping. I create TypeMap
, add Converter
and for LocalDateTime
a Provider
also since it does note have public default constructor. Something like here.
If I had in my DTO also LocalDateTime createdTime
(or String createdTime
in my Entity
) then ModelMapper.validate()
would be happy. But I do not have and I need to create all the converting stuff.
All this leads to ModelMapper.validate()
to complain:
Unmapped destination properties found in TypeMap[DTO -> Entity]:
org.example.test.modelmapper.validation.TestIt$Entity.setCreatedTime()
The code I currently use for validating mapping for LocalDateTime
case is:
ModelMapper mm = new ModelMapper();
mm.createTypeMap(Entity.class, DTO.class);
mm.createTypeMap(DTO.class, Entity.class);
mm.createTypeMap(String.class, LocalDateTime.class)
.setPropertyProvider(localDateTimeProvider);
mm.addConverter(toStringDate);
mm.validate();
(so I am not doing any actual mapping but validating the mapping)
with
Provider<LocalDateTime> localDateTimeProvider =
new AbstractProvider<LocalDateTime>() {
@Override
public LocalDateTime get() {
return LocalDateTime.now();
}
};
and
Converter<String, LocalDateTime> toStringDate = new AbstractConverter<>() {
@Override
protected LocalDateTime convert(String source) {
return LocalDateTime.parse(source);
}
};
Ask for more details/code. I'll update question as needed