Question

Suppose I have a source object, containing two properties, a and b like this:

public class Source {
    String a, b;
}

and a target object with one property c:

public class Target {
    String c;
}

I'd like to define a mapping that will:

  1. map property a to c if a is not null
  2. map property b to c if b is not null

I thought it would be possible with a mapper with mapNulls set to false:

factory.registerClassMap(factory.classMap(Source.class, Target.class).field("a", "c").field("b", "c").mapNulls(false));

However, when I set property a to a non-null value and leave property b to null, the mapping results in a target object with c set to null.

Have I misunderstood the purpose of mapNulls?

Was it helpful?

Solution

It's because mapNulls set the property at the last field. Try setting at both fields.
You can implement a ConfigurableMapper too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top