I'm using MapStruct to make dto <-> entity
mapping. The same mappers are used to create and update entities from dtos. A verification of the dto's id is done to know whether a new entity must be created (id == null) or it should be retrieved from database (id != null) .
I'm actually using MapperDecorator as a workaround. Example :
Mapper
@Mapper
@DecoratedWith(UserAccountDecorator.class)
public interface UserAccountMapper {
UserAccountDto map(User user);
User map(UserAccountDto dto);
User map(UserAccountDto dto, @MappingTarget User user);
}
Decorator
public abstract class UserAccountDecorator implements UserAccountMapper {
@Autowired
@Qualifier("delegate")
private UserAccountMapper delegate;
@Autowired
private UserRepository userRepository;
@Override
public User map(UserAccountDto dto) {
if (dto == null) {
return null;
}
User user = new User();
if (dto.getId() != null) {
user = userRepository.findOne(dto.getId());
}
return delegate.map(dto, user);
}
}
But this solution becomes heavy due to the fact that a decorator must be created for each mapper.
Is there any good solution to perform that ?
I'm using :
- MapStruct : 1.1.0