Subbies
Subbies

Reputation: 264

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.

Upvotes: 1

Views: 1293

Answers (1)

Subbies
Subbies

Reputation: 264

Ok so I seemed to have worked it out. I now have

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

Most likely my project wasn't building correctly and my corrections weren't being deployed.

Upvotes: 3

Related Questions