Dozer is a Java Bean to Java Bean mapper that recursively copies data from one object to another. Typically, these Java Beans will be of different complex types. This blog will explain you how to convert one Java Bean into another Java Bean by using context type mapping[Dozer mappings], also you can convert one variable type into another variable type by defining them into XML file.

Dozer supports different type of mappings:
1. Simple property mapping
2. Complex type mapping
3. Bi-directional mapping
4. implicit-explicit mapping
5. Recursive mappings
Dozer supports mapping for collection attributes, which is required to element level mapping.
Many a times we need a context based mapping with Dozer so as to control number of fields conversion. Let’s take an example of converting

SourceClass to DestinationClass.

public class SourceClass {                         
	private String sourceStringForInteger;
	private String sourceStringForLong; 
	private String sourceStringForint;
	private String sourceStringForString;
	private List sourceCol;
	…
}

public class DestinationClass {
	private Integer sourceStringForInteger;
	private Long sourceStringForLong;
	private int sourceStringForint;
	private String sourceStringForString;
	private List destCol;
	…
}

You can see SourceClass contains sourceCol variable of type List, which we are converting into destCol variable of type List in DestinationClass.
Here sourceCol is an object type of SourceChildClass and destCol is an object of DestinationChildClass.

public class SourceChildClass { 
	private String prop1; 
	private String prop2; 
	private String prop3; 
	…
}

public class DestinationChildClass {
	private Integer prop1;
	private Integer prop2;
	private Integer prop3;
	…
}

Usecase 1 [Simple conversion of source object into destination object.]

We will use map-id attribute to name the cases.
In one service call/conversion we want to convert SourceClass into DestinationClass (say caseA)

<mapping map-id="caseA">
      <class-a>package.SourceClass</class-a>
      <class-b>package.DestinationClass</class-b>
      <field>
       <a>sourceCol</a>
       <b>destCol</b>
       <a-hint>package.SourceChildClass</a-hint>
       <b-hint>package.DestinationChildClass</b-hint>
      </field>
</mapping>

In this mapping

1. Tag <class-a> value source class
2. Tag <class-b> value destination
3. Tag <a> value collection variable in source class
4. Tag <b> value collection variable in destination class
5. Tag <a-hint> Object type of collection defines as tag <a> value.
6. Tag <b-hint> Object type of collection defines as tag <b> value.
7. Other SourceClass fields will implicitly convert into DestinationClass fields irrespective of there data types, like sourceStringForInteger a variable of String type in SourceClass, dozer will convert this variable into Integer type in DestinationClass.

Let’s see we want to exclude sourceStringForInteger variable define SourceClass (say caseB).

<mapping map-id="caseB">
      <class-a>package.SourceClass</class-a>
      <class-b>package.DestinationClass</class-b>
      <field>
       <a>sourceCol</a>
       <b>destCol</b>
       <a-hint>package.SourceChildClass</a-hint>
       <b-hint>package.DestinationChildClass</b-hint>
      </field>

<field-exclude>	
         	<a> sourceStringForInteger </a>
         	<b> sourceStringForInteger </b>
          </field-exclude>
   </mapping>

In this mapping
Add tag and define variable names, which you want to exclude.

Let’s see how code look like. sourceClass is an object of type SourceClass which has to be converted into DestinationClass

DestinationClass destinationClass = (DestinationClass) mapper.map (sourceClass, DestinationClass.class,"caseA");

Definition of mapper is done as bean in Spring.

Here we pass a third string argument indicating the name of the mapping to be used by the Dozer. This argument will be used for converting from SourceClass to DestinationClass.
While doing reverse conversion means conversion form DestinationClass to SourceClass we have to add PRIME to name of mapping.

SourceClass sourceClass = (SourceClass) mapper.map (destinationClass, SourceClass.class,"caseAPRIME");

Usecase 2 [Complex conversion of source object into destination object.]

Let’s see a complex scenario. Say we want to exclude prop1 variable defined in SourceChildClass to be converter for caseA. So for this we have to tell the dozer to use a different mapping while converting the object belonging to collection. We could name that mapping as collCaseA, and in the field tag we will use map-id to indicate the name.

<mapping map-id="caseA">
      <class-a>package.SourceClass</class-a>
      <class-b>package.DestinationClass</class-b>
       <! -- Uses mapping named childCaseA for conversion of objects in the collection -- >
      <field map-id="collCaseA">
       <a>sourceCol</a>
       <b>destCol</b>
       <a-hint>package.SourceChildClass</a-hint>
       <b-hint>package.DesctinationChildClass</b-hint>
      </field>
 </mapping>

 <mapping map-id="collCaseA">
      <class-a>package.SourceChildClass</class-a>
      <class-b>package.DestinationChildClass</class-b>
      <field-exclude>
         <a>prop1</a>
         <b>prop1</b>
      </field-exclude>
 </mapping>

In this mapping
The change done is from Usecase-1, CaseA in adding new mapping to define exclude specific field from collection conversion under < mapping map-id="collCaseA"> tag.

Similarly for caseB we want to exclude prop2 variable define in SourceChildClass, so we call it as collCaseB.

<mapping map-id="caseB">
  <class-a>package.SourceClass</class-a>
  <class-b>package.DestinationClass</class-b>
  <field map-id="collCaseB">
   <a>sourceCol</a>
   <b>destCol</b>
   <a-hint>package.SourceChildClass</a-hint>
   <b-hint>package.DestinationChildClass</b-hint>
  </field>
  <field-exclude>
   <a>sourceStringForInteger</a>
   <b>sourceStringForInteger</b>
  </field-exclude>
</mapping>

<mapping map-id="collCaseB">
    <class-a>package.SourceChildClass</class-a>
    <class-b>package.DestinationChildClass</class-b>
    <field-exclude>
      <a>prop2</a>
      <b>prop2</b>
    </field-exclude>
</mapping>

In this mapping
The change done is from Usecase-1, CaseB in adding new mapping to define exclude specific field from collection conversion under <mapping map-id="collCaseB"> tag.

Conversion code would remain same for Usecase 2 as in Usecase 1.

Conclusion: You have seen various cases of context based mapping conversion. For more information you can refer following resources.
Deep Mappings
http://dozer.sourceforge.net/
If you have any specific question, please comment back. I will try to reply with answer.