Search code examples
javamappingdozer

Dozer - map child elements


I have 2 Java objects, each contains a List of different Java objects. Using Dozer, I need to map the data from one object to another object. Can anyone help me pa the data from the FromPerson object to the ToPerson object? I am able to map the personList (6 in total) but not its fields.

class From{
List<FromPerson> personList;
}

class FromPerson{
String name;
}

class To{
List<ToPerson> personList;
}

class ToPerson{
String toPersonName;
}

    <mapping>
        <class-a>com.From</class-a>
        <class-b>com.To</class-b>
    </mapping>

Solution

  • Since both are list , if you are sure about first object of fromPerson list should map to first object of toPerson list, it is possible,Please see below code, it is working code

     <mapping>
        <class-a>com.FromPerson</class-a>
        <class-b>com.ToPerson</class-b>
        <field>
            <a>name</a>
            <b>toPersonName</b>
        </field>
    </mapping>
    <mapping>
        <class-a>com.From</class-a>
        <class-b>com.To</class-b>
        <field>
            <a>personList</a>
            <b>personList</b>
            <b-hint>com.ToPerson</b-hint>
        </field>
    </mapping>