According to the Dozer documentation:
How do I map multiple fields to a single field?
Dozer doesn't currently support this. And because of the complexities around ? implementing it, this feature is not currently on the road map. A possible solution would be to wrap the multiple fields in a custom complex type and then define a custom converter for mapping between the complex type and the single field. This way, you could handle the custom logic required to map the three fields into the single one within the custom converter.
If you have control over your legacy data, you could create a type to wrap the individual date fields:
MyWrapperObject.java
public class MyWrapperObject {
private int day;
private int month;
private int year;
}
MyObject.java
public class MyObject {
private MyWrapperObject myWrapperObject;
}
and then use a custom converter to map the 3 fields into a date field:
MyOtherWrapperObject.java
public class MyOtherWrapperObject {
private Date date;
}
DateCustomConverter .java
public class DateCustomConverter implements CustomConverter {
@Override
public Object convert(Object destination, Object source,
@SuppressWarnings("rawtypes") Class destClass,
@SuppressWarnings("rawtypes") Class sourceClass) {
// Source object is null
if (source == null) {
return null;
}
if (source instanceof MyWrapperObject) {
MyOtherWrapperObject dest = new MyOtherWrapperObject();
MyWrapperObject src = (MyWrapperObject) source;
Calendar c = Calendar.getInstance();
// Months are 0 based in Java
c.set(src.getYear(), src.getMonth() - 1, src.getDay());
dest.setDate(c.getTime());
return dest;
}
return null;
}
Then reference this converter in your XML mapping file:
dozer.xml
<mapping map-null="false" >
<class-a>com.xxx.MyObject</class-a>
<class-b>com.xxx.MyOtherObject</class-b>
...
<field custom-converter="com.xxx.DateCustomConverter">
<a>myWrapperObject</a>
<b>myOtherWrapperObject</b>
</field>
</mapping>
If you don't have control over your legacy data then I believe you'd have to write a custom mapper to map MyObject
to MyOtherObject
.