Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dozer mapping : More than one source to destination

Tags:

java

dozer

Im new to DOZER mapping

Can we map properties from more than one source class to destination?

EG

class A {
          int a;
          int b;
}

class B {
    String c;
}

class Destination {
    int a;
    int b;
    String c;
}

Can it be possible to do this with one mappings configuration file ?

like image 315
Vinay Veluri Avatar asked Dec 02 '22 20:12

Vinay Veluri


1 Answers

You can just map twice. First, use Destination.class as target, then use the Object that resulted from the first mapping as target:

    One one = new One();
    one.setA(1);
    one.setB(2);

    Two two = new Two();
    two.setC("3");

    Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();

    Destination destination = mapper.map(one, Destination.class);

    mapper.map(two, destination);

    System.out.println(destination);
    // Destination [a=1, b=2, c=3]

This even works with an empty mapping configuration file.

like image 191
Holgzn Avatar answered Dec 04 '22 08:12

Holgzn