I want to do a partial update on one of my entities but if one propertie is null then the entity to be updated gets that value set to null too. I want that if a property from the source is null then to keep the one from the source.
I have tried this but no luck:
@Bean
public ModelMapper modelMapper() {
ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration().setPropertyCondition(Conditions.isNotNull());
modelMapper.createTypeMap(String.class, Date.class);
modelMapper.addConverter(new StringToDate());
modelMapper.addConverter(new DateToString());
return modelMapper;
}
Then I update my object like this:
@Override
public void editUser(final User user) {
UserDocument userDocument = this.usersRepository.findByIdAndActivo(user.getId(), true)
.orElseThrow(UserNotFoundException::new);
userDocument = this.modelMapper.map(user, UserDocument.class);
this.usersRepository.save(userDocument);
}
The user
object has 1 property set at null while the object userDocument
has it with a value, then when I save it in the database that value is gone (because it has transformed into null).
What can be wrong?
Thanks.