Search code examples
javamappingdozer

Dozer mapping of Lists of Complex Object


Is there a way to map one List of objects to one List of objects ids? I have the following objects:

public class Role implements Serializable {
    private List<Section> sections;
    //getters and setters
    ...
}

public class Section {
    private Long id;
    //getters and setters
    ...
}

public class RoleDTO implements Serializable {
    private List<Long> sections;
    //getters and setters
    ...
}

How can i map this with Dozer XML?


Solution

  • You can use a custom converter

    Dozer XML

    <field custom-converter-id="mySectionsConverter">
      <a>sections</a>
      <b>sections</b>
    </field>
    

    Spring XML

    <bean id="mapper" class="org.dozer.spring.DozerBeanMapperFactoryBean">
        <property name="mappingFiles" value="..." />
        <property name="customConvertersWithId">
            <map>
               <entry key="mySectionsConverter" value-ref="..." />
            </map>
        </property>
    </bean>
    

    Note: I'm just in the process of removing dozer from my application because I feel that it complicates things. In my opinion a simple java POJO converter class does a much better job than all this XML, custom converters and spring wiring. I've also found cases where it was impossible to reuse a value in a nested converter which caused multiple database hits which weren't required with the POJO solution.