简体   繁体   中英

Mapping two values to a single property with Orika

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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