【问题标题】:ModelMapper not ignoring null valuesModelMapper 不忽略空值
【发布时间】:2019-05-29 11:21:40
【问题描述】:

我想对我的一个实体进行部分更新,但如果一个属性为 null,那么要更新的实体也会将该值设置为 null。我希望如果源中的属性为空,则保留源中的属性。

我试过了,但没有运气:

    @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;
    }

然后我像这样更新我的对象:

    @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);
    }

user 对象有 1 个属性设置为 null,而对象 userDocument 有一个值,然后当我将它保存在数据库中时,该值消失了(因为它已转换为 null)。

有什么问题?

谢谢。

【问题讨论】:

  • 设置 userDocument = 将其分配给对 ModelMapper.map 输出的引用,因此丢失了之前在其中的任何信息(当它引用不同的位置时)。如果要保留内容并复制非空值,则需要创建一个 USerDocument 方法,该方法将另一个 UserDocument 作为参数,并将参数的非空字段更新为“this”
  • 那么这个配置的目的是什么? modelMapper.getConfiguration().setPropertyCondition(Conditions.isNotNull()) @JeremyKahan
  • 这是一个公平的问题,我不确定。哦,我看你想通了。它有助于合并,但不能创造新鲜的东西。

标签: java spring spring-boot modelmapper


【解决方案1】:

好的,所以配置不是我认为的目的。

我已经通过将更新的对象与旧的对象合并解决了这个问题,如下所示:

    @Override
    public void editUser(final User user) {
        UserDocument userDocument = this.usersRepository.findByIdAndActivo(user.getId(), true)
                .orElseThrow(UserNotFoundException::new);

        this.modelMapper.map(user, userDocument);
        this.usersRepository.save(userDocument);
    }

【讨论】:

    【解决方案2】:

    你可以这样解决:

    @Configuration
    public class ModelMapperConfig {
    
        @Bean
        public ModelMapper modelMapper() {
            ModelMapper modelMapper = new ModelMapper();
            modelMapper.getConfiguration().setSkipNullEnabled(true);
    
            return modelMapper;
        }
    }
    

    【讨论】:

      【解决方案3】:

      Mybatis 框架?你没有设置选择性保存 --selective

      <insert id="insertSelective" parameterType="com.zjl.domain"> insert into table_name <trim prefix="(" suffix=")" suffixOverrides=","> <if test="id != null"> id, </if> <if test="createDate != null"> create_date, </if> <if test="modifiedDate != null"> modified_date, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="id != null"> #{id,jdbcType=BIGINT}, </if> <if test="createDate != null"> #{createDate,jdbcType=TIMESTAMP}, </if> <if test="modifiedDate != null"> #{modifiedDate,jdbcType=TIMESTAMP}, </if> </trim> </insert>

      【讨论】:

      • 不,它是 MongoDB。无论如何,我认为这不是问题。当源的字段为空时,它应该使用目标的字段
      猜你喜欢
      • 2014-03-27
      • 2019-02-27
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 2019-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多