Menu

#368 Mapping between two beans with maps

Dozer v5.3.2
open
nobody
5
2012-02-27
2012-02-27
Andre Onuki
No

I wan to a map between one certain key of a maps in one object to another certain key in a map in another object.
But dozer will see that both objects are maps and will instead copy the whole map, ignoring the mapKey property I give.

Here's a test that show the expected result, commented to show what dozer does.

public class BeanA {
private Map<String, String> map;
public Map<String, String> getMap() {
if (this.map == null) {
this.map = new HashMap<String, String>();
}
return this.map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
}

public class BeanB {
private Map<String, String> map;
// setter and getter as BeanA
}

public void test() {
BeanMappingBuilder mappingBuilder = new BeanMappingBuilder() {

@Override
protected void configure() {
FieldDefinition def1 = new FieldDefinition("map");
def1.mapKey("value");

FieldDefinition def2 = new FieldDefinition("map");
def2.mapKey("newValue");

this.mapping(BeanA.class, BeanB.class).fields(def1, def2);

}
};

DozerBeanMapper mapper = new DozerBeanMapper();
mapper.addMapping(mappingBuilder);

BeanA a = new BeanA();
a.getMap().put("value", "expected");
a.getMap().put("value2", "another value");

BeanB b = mapper.map(a, BeanB.class);

// this assert fails as b.getMap contains: {value=>expected; value2=>another value}
Assert.assertNotNull(b.getMap().get("newValue"));

// what I really wanted was b.getMap to contain only the key that was given: {newValue=>expected}
Assert.assertEquals("expected", b.getMap().get("newValue"));
}

Discussion


Log in to post a comment.