简体   繁体   English

是否可以对 ModelMapper(在 spring 引导中)使用一个 propertyMap 到 map 在两个方向(DTO 到实体和实体到 DTO)?

[英]Is it possible to use one propertyMap for ModelMapper (in spring boot) to map in both directions (DTO to Entity and Entity to DTO)?

Good Morning, i used Model Mapper in spring boot to convert my Entity to a DTO ( i configured Model Mapper buy setting the correct property Map) but i couldn't use the same property Map to convert the same DTO to the Entity. Good Morning, i used Model Mapper in spring boot to convert my Entity to a DTO ( i configured Model Mapper buy setting the correct property Map) but i couldn't use the same property Map to convert the same DTO to the Entity.

I think that one Property Map can be enough to do the mapping in both directions: my Map:我认为一个属性 Map 足以在两个方向上进行映射:我的 Map:

public class UserPropertyMap extends PropertyMap<UserEntity, UserDTO> {
    @Override
    protected void configure() {
        map().setId_user(source.getId());
        map().setName(source.getName());
        map().setId_profile(source.getProfile().getId());
    }
}

thanks in advance提前致谢

With the current ModelMapper Version 2.4.2 it is not possible I'm afraid.使用当前的 ModelMapper 版本 2.4.2 恐怕是不可能的

The reason is It's hard to define how ModelMapper should map destination to source if it only know how to map source to destination.原因是如果 ModelMapper 只知道如何从 map 源到目标,则很难定义 ModelMapper 应该如何从 map 目标到源。

For example, if we had a converter toUpperCase using to map entity.firstName to dto.firstName like this using(toUpperCase).map(entity.firstName, dto.firstName) .例如,如果我们有一个转换器toUpperCase使用 to map entity.firstNamedto.firstName像这样using(toUpperCase).map(entity.firstName, dto.firstName)
How can we reverse-map this?我们怎样才能反向映射呢?

There is a related github issue .有一个相关的github 问题 Check that out!检查出!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM