Collection Mapping

2,688 views
Skip to first unread message

kiwi273

unread,
Dec 8, 2011, 10:06:22 PM12/8/11
to modelmapper
Hi There,

Does modelMapped allow collection mapping?

Eg:

public class User{

string name;
List<Role> roles = new ArrayList<Role>;
......

}

public class Role{

string name;
string description
}


I would like to map dataobject USER to DTO USER. How do I map
user.roles data object to DTO's user.roles.

Ninju

unread,
Dec 13, 2011, 12:01:45 PM12/13/11
to modelmapper
I too am interesting in mapping a collection of POJOs

Have you found any support for this currently.

If not I would be interested in implementing it

Jonathan Halterman

unread,
Jul 25, 2012, 7:28:45 PM7/25/12
to model...@googlegroups.com
Yes - ModelMapper supports Collection mapping. If the source and destination object graphs are similar, ModelMapper will automatically map everything for you. If they're a bit different, where properties from the source side are moved into a collection somewhere else on the destination side, you may need to define a custom PropertyMap.

Jon Lorusso

unread,
Dec 9, 2012, 9:54:04 AM12/9/12
to model...@googlegroups.com

This thread is a bit old, but I'd like some clarification if possible. I have the following object:

Configuration {
   List<Location> locations
}

which I would like to map to:

ConfigurationDTO {
  List<Long> locationIds
}

Is this possible using modelmapper?  I've tried with the default configuration and that does not seem to work, as well as using a PropertyMap and a Converter.

In the former case I get an exception about not being able to instantiate a List, and in the latter an exception about not being able to cast

org.modelmapper.internal.cglib.empty.Object$$EnhancerByModelMapper$$b0cc9d40

as a Location.


Do you have any suggestions? 

Thanks,

--jon
Message has been deleted

Jonathan Halterman

unread,
Dec 10, 2012, 4:29:52 PM12/10/12
to model...@googlegroups.com
Hi Jon,

Here's one approach:

    modelMapper.addMappings(new PropertyMap<Configuration, ConfigurationDTO>() {
      @Override
      protected void configure() {
        Converter<List<Location>, List<Long>> locationConverter = new Converter<List<Location>, List<Long>>() {
          public List<Long> convert(MappingContext<List<Location>, List<Long>> context) {
            List<Long> result = new ArrayList<Long>();
            for (Location loc : context.getSource())
              result.add(loc.id);
            return result;
          }
        };
        
        using(locationConverter).map(source.getLocations()).setLocationIds(null);
      }
    });


There's two things we need to accomplish. The first is making source that Confirmation.locations maps to configurationDTO.locationIds. We can do that by creating a mapping inside a PropertyMap:

map(source.getLocations()).setLocationIds(null);

But of course this will fail since getLocations returns a different type then what setLocationIds is expecting. So we include a converter that converts between those two types.

Cheers,

Jonathan

Jonathan Halterman

unread,
Dec 10, 2012, 4:53:15 PM12/10/12
to model...@googlegroups.com
I should also mention, as an alternative to creating a Converter for that specific property mapping, you can create a more general purpose Converter between Location and Long:

    modelMapper.addConverter(new Converter<Location, Long>() {
      public Long convert(MappingContext<Location, Long> context) {
        return context.getSource().id;
      }
    });

Note, there was an issue effecting converters to types such as Long which is fixed in the current master branch, but has been released yet.

This this approach you'll still need a mapping from Configuration.locations->Configuration.setLocationIds:

map(source.getLocations()).setLocationIds(null);

Cheers,
Jonathan  
Reply all
Reply to author
Forward
0 new messages