首页 > 解决方案 > Using MapStruct to fill in same classes

问题描述

I am trying to use MapStructs to fill an object with values of a second object of the same class. My issue is that the source has some null values that are updating existing values in the target.

Class A{
    Integer one;
    Integer two;
}

@Mapper(componentModel="cdi")
Class Mapper{
   public abstract void fill(A source, @MappingTarget A target);
}

@Test
void test(){
  var source = new A(1, null);
  var target = new A(null, 2);
  mapper.fill(source, target);
 //expected A(1,2) but get A(1, null)
}

is basically what I have. I've tried adding NullValuePropertyMappingStrategy.IGNORE to the Mapper, as well as annotating the fill method with @BeanMapping to add the NullValuePropertyMappingStrategy.IGNORE as well as the NullValueCheckStrategy.ALWAYS but was unsuccessful.

标签: javamapstruct

解决方案


好的,所以我似乎已经解决了。我现在有

@Mapper(componentModel="cdi")
Class Mapper{
@BeanMapping(
nullValuePropertyMappingStrategy=NullValuePropertyMappingStrategy.IGNORE, 
nullValueCheckStrategy=NullValueCheckStrategy.ALWAYS)
   public abstract void fill(A source, @MappingTarget A target);
}

很可能我的项目没有正确构建,并且我的更正没有被部署。


推荐阅读