Search code examples
javadozer

Dozer mapping : More than one source to destination


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 ?


Solution

  • Not directly no. You would need to either create a new class to wrap around your two source classes and copy from that:

    class D {
        private A a;
        private B b;
    }
    
    <mapping>
      <class-a>D</class-a>
      <class-b>C</class-b>
      <field>
        <a>a.a</a>
        <b>a</b>
      </field>
      <field>
        <a>a.b</a>
        <b>b</b>
      </field>
      <field>
        <a>b.c</a>
        <b>c</b>
      </field>
    </mapping>
    

    Or you would need to copy twice, once from each source class to the destination object, making sure not to blank out existing fields.

    <mapping wildcard="false">
        <class-a>A</class-a>
        <class-b>C/class-b>
        <field>
           <a>a</a>
           <b>a</b>
        </field>   
        <field>
           <a>b</a>
           <b>b</b>
        </field>   
    </mapping>
    
    <mapping wildcard="false">
        <class-a>B</class-a>
        <class-b>C/class-b>
        <field>
           <a>c</a>
           <b>c</b>
        </field> 
    </mapping>