Map a dto to an entity retrieved from database if Dto has Id using MapStruct

安稳与你 提交于 2020-05-23 04:54:17

问题


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 :

  1. MapStruct : 1.1.0

回答1:


I solved my problem by following the advice of Gunnar in the comment.

I moved to MapStruct 1.2.0.Beta1 and created a UserMapperResolver like below

@Component
public class UserMapperResolver {

    @Autowired
    private UserRepository userRepository;

    @ObjectFactory
    public User resolve(BaseUserDto dto, @TargetType Class<User> type) {
        return dto != null && dto.getId() != null ? userRepository.findOne(dto.getId()) : new User();
    }

}

Which I use then in my UserMapper :

@Mapper(uses = { UserMapperResolver.class })
public interface BaseUserMapper {

    BaseUserDto map(User user);

    User map(BaseUserDto baseUser);

}

The generated code is now :

@Override
    public User map(BaseUserDto baseUser) {
        if ( baseUser == null ) {
            return null;
        }

        User user = userMapperResolver.resolve( baseUser, User.class );

        user.setId( baseUser.getId() );
        user.setSocialMediaProvider( baseUser.getSocialMediaProvider() );
...
}

Works well !




回答2:


MapStruct alone can't do that. However, with some generics and a main abstract class you can make your life easier.

You need one generic interface. It must not be annotated with @Mapper, because if it is MapStruct will try to generate an implementation and it will fail. It cannot generate generic mappers.

public interface GenericMapper<E, DTO> {

    DTO map(E entity);

    E map(DTO dto);

    E map(DTO dto, @MappingTarget E entity);
}

Then you need one abstract class where you'll have your logic.

public abstract class AbstractGenericMapper<E, DTO> implements GenericMapper<E, DTO> {

    @Autowired
    private Repository<E> repository;

    @Override
    public final E map (DTO dto) {
        if (dto == null) {
            return null;
        }

        // You can also use a Java 8 Supplier and pass it down the constructor
        E entity = newInstance();
        if (dto.getId() != null) {
            user = repository.findOne(dto.getId());
        }

        return map(dto, entity);
    }

    protected abstract E newInstance();
}

And then each of your mappers will only need to extend this abstract class.

@Mapper
public abstract class UserAccountMapper extends AbstractGenericMapper<User, UserDto> {

    protected User newInstance() {
        return new User();
    }
}

MapStruct will then generate an implementation for your mapper and you will only have to extend the AbstractGenericMapper for the future. Of course you will need to adapt the generic parameters so you can at least get the id from the via some interface maybe. If you have different type of ids then you will have to add that generic parameter to the AbstractGenericMapper as well.



来源:https://stackoverflow.com/questions/42367081/map-a-dto-to-an-entity-retrieved-from-database-if-dto-has-id-using-mapstruct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!