How to map bean to java.util.Map with BeanMappingBuilder?

237 views
Skip to first unread message

Alfonso Mateos Alarcón

unread,
Dec 12, 2014, 7:35:30 AM12/12/14
to dozer-...@googlegroups.com
Hi guys, I managed to map from my bean to a Map<String, Object> by using XML configuration, but not through API. What else should I do to get my java-based mapping working?
Thanks in advance ;-)

My working XML is:

<mapping>

     <class-a>java.util.Map</class-a>

     <class-b>es.gc.epsilon.core.dto.processes.spain.RecruitingPersonDto</class-b>

</mapping>


My non-working java code is:

        DozerBeanMapper mapper = new DozerBeanMapper();

        mapper.addMapping(new BeanMappingBuilder() {

            @Override

            protected void configure() {

                mapping(

                type(RecruitingPersonDto.class),

                type(Map.class),

                mapId("A"),

                mapNull(true)

                );

            }

        });

        Map<String, Object> map = new LinkedHashMap<String, Object>();

        mapper.map(rPersonDto, map);

Philipp Schneider

unread,
Jan 9, 2015, 5:33:43 AM1/9/15
to dozer-...@googlegroups.com
Hello Alfonso,
I can not help you, sorry.
But I have a simular problem.
I also want to map from java.util.Map to a PoJo Object with properties.
Since you wrote that with xml mapping it is working, could you please provide more of your working code. 
Like the source/destination classes and how you use the xml mapping inside the code.

That may help me and I may figure out how to configure it with the API.

Thanks a lot

Joaquin Montesinos

unread,
Nov 10, 2015, 11:00:24 PM11/10/15
to Dozer Mapper

You need to define the map side entry as FieldDefinition instead of using the name directly:

e.g.:


protected void configure() {

                mapping(

                type(RecruitingPersonDto.class),

                type(Map.class),

                mapId("A"),

                mapNull(true)

                ).fields("myEntityField", new FieldDefinition("this").mapKey("myMapKey"));

            }


And repeat with all the fileds in the map.
Reply all
Reply to author
Forward
0 new messages