I want to map an entity to a bean class using a model mapper since they both have identical fields except for additional property in the entity class, which is an object type. It works for Dto to entity conversion, however, the additional property in this case returns null.
Bean/ Dto class
public class SettingDto {
private UUID settingUUID;
private AdditionalPropertyDto additionalProperty;
}
AdditionalPropertyDto Class
public class AdditionalPropertyDto {
private String colorCode;
}
Entity class
public class SettingEntity {
@Id
@Column(name = "setting_uuid", nullable = false)
private UUID settingUUID;
@Type(type = "jsonb")
@Column(name = "additional_property", columnDefinition = "jsonb")
private Object additionalProperty;
}
When I use this approach, the AdditionalPropertyDto class returns null, and only the UUID obtains mapping.
public SettingDto apply(SettingEntity settingEntity) {
return mapper.map(settingEntity, SettingDto.class);
}
Use ObjectMapper separately for additionalProperty setting
Like this
public SettingDto apply(SettingEntity settingEntity) {
SettingDto settingDto = mapper.map(settingEntity, SettingDto.class);
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String json = null;
ObjectMapper mapper = new ObjectMapper();
try {
json = ow.writeValueAsString(settingEntity.getAdditionalProperty());
settingDto.setAdditionalProperty( mapper.readValue(json, AdditionalPropertyDto.class));
} catch (JsonProcessingException e) {
LOGGER.debug("Error Converting entity to dto...");
}
return settingDto;
}