I have two beans, one has five attributes and the other has four. How do I configure the mapping if I simply want to ignore the extra attribute?
All of the exclude examples I see take an argument for both class one and class two.
You shouldn't need to do anything; Dozer should handle this out the box.
Consider the following two beans:
Source.java
public class Source {
private String field1;
private String field2;
private String field3;
// Getters and setters omitted
}
Destination.java
public class Destination {
private String field1;
private String field2;
// Getters and setters omitted
}
You can use the following mapping in your dozer.xml file (this will map all properties where the names match in both beans):
<mapping>
<class-a>com.example.Source</class-a>
<class-b>com.example.Destination</class-b>
</mapping>
The following unit tests show that the mapping works both ways:
@Test
public void sourceToDestination() {
List<String> mappingFiles = new ArrayList<String>();
mappingFiles.add("dozer.xml");
this.beanMapper = new DozerBeanMapper(mappingFiles);
Source source = new Source();
source.setField1("A");
source.setField2("B");
source.setField3("C");
Destination dest = beanMapper.map(source, Destination.class);
assertEquals("A", dest.getField1());
assertEquals("B", dest.getField2());
}
@Test
public void destinationToSource() {
List<String> mappingFiles = new ArrayList<String>();
mappingFiles.add("dozer.xml");
this.beanMapper = new DozerBeanMapper(mappingFiles);
Destination dest = new Destination();
dest.setField1("A");
dest.setField2("B");
Source source = beanMapper.map(dest, Source.class);
assertEquals("A", source.getField1());
assertEquals("B", source.getField2());
assertNull(source.getField3());
}
When we map Source to Destination, as Destination does not have a property named field3
it is ignored. When we map the other way (Destination to Source), field3
in Source is null.