Map multiple sources to a single destination

2,301 views
Skip to first unread message

Udi Ben Amitai

unread,
Jul 5, 2014, 5:14:52 PM7/5/14
to model...@googlegroups.com
Hi,
I just started using ModelMapper, so I'm sorry if this a trivial question.
I want to map multiple domain object to a single DTO object:
    public class Name{
        private String name;
    }

    public class Id{
        private String id;
    }

    public class Dto{
        public String name;
        public String id;
    }

(Assume getter/setters..)
I want to map both types (Name and Id) to the Dto.

The type mapping  (and even the mapping) works fine:
modelMapper.createTypeMap(IdObj.class, Dto.class);
modelMapper.createTypeMap(NameObj.class, Dto.class);

But validate() fails as it expect each of the sources to fulfill all destination's fields.
Is that the recommended practice for such case?
Is there a way (configuration?) to make validation ignore missing properties in the destination?

Thanks,
Udi

Jonathan Halterman

unread,
Jul 5, 2014, 6:01:43 PM7/5/14
to model...@googlegroups.com
Hi Udi,

In this case validate won't work since all of the destination properties won't be fulfilled by a single source type. You can certainly map this scenario though:

Dto dto = modelMapper.map(IdObj.class, Dto.class);
modelMapper.map(NameObj.class, dto);

Cheers,
Jonathan


--
You received this message because you are subscribed to the Google Groups "modelmapper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to modelmapper...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages