Mapping from Map within a Map

96 views
Skip to first unread message

Alan Balinju Cassar

unread,
May 27, 2015, 10:00:49 AM5/27/15
to dozer-...@googlegroups.com
I have an input which consists of a Map. One of the fields within this map, is another Map.

I am trying to map a field from the inner map, to my destination object like the following:

<mapping map-null="false">
        <class-a>java.util.Map</class-a>
        <class-b>DesClass</class-b>
        <field>
         <a>ai.aiInner</a>
         <b>aiResult</b>
        </field>
</mapping>

This never sets my aiResult field, because from what I can see from my debugging, Dozer will try to do the following: input.get("ai.aiInner"), treating ai.aiInner as the name of a field.

I tried also setting <a-hint>java.util.Map</a-hint> and <a-deep-index-hint>java.util.Map</a-deep-index-hint>, but still same result.

I also tried the following:

<mapping map-null="false">
        <class-a>java.util.Map</class-a>
        <class-b>DesClass</class-b>
        <field>
        <a key="aiInner">ai</a>
        <b>aiResult</b>
        </field>
</mapping>

However this is throwing an error saying that it cannot find a getter for "ai" in call java.util.LinkedHashMap.

Any ideas?
Message has been deleted
Message has been deleted

Tausif Farooqi

unread,
Sep 9, 2015, 1:48:16 PM9/9/15
to Dozer Mapper
I have the same problem. Did you find any easy solution for this?

I couldn't find a direct way to do this, so I did it by adding a custom converter that would extract the inner map first and then apply the mapping.

The mapping will look something like this:

<mapping map-null="false">
        <class-a>java.util.Map</class-a>
        <class-b>WrapperClass</class-b>
        <field custom-converter="mypackage.ConverterClass">
         <a>ai</a>
         <b>aiResult</b>
        </field>
</mapping>

<mapping map-null="false"  map-id="innerMapMapper">
        <class-a>java.util.Map</class-a>
        <class-b>DesClass</class-b>
        <field>
         <a>innerMapKey1</a>
         <b>aiResultField1</b>
        </field>
        <field>
         <a>innerMapKey2</a>
         <b>aiResultField2</b>
        </field>
</mapping>

And the class will look like this:

public class ConverterClass extends DozerConverter<Map, DesClass> implements MapperAware {
private Mapper mapper;

public ConverterClass() {
super(Map.class, DesClass.class);
}

@Override
public DesClass convertTo(Map source, DesClass destination) {
if (source != null) {
Map innerMap = source.get("aiInner");
return mapper.map(innerMap , DesClass.class, "innerMapMapper");
}
return null;
}

@Override
public Map convertFrom(DesClass source, Map destination) {
// add reverse conversion code
}
@Override
public void setMapper(Mapper mapper) {
this.mapper = mapper;
}
}
Reply all
Reply to author
Forward
0 new messages