ModelMapper: An Object To Object Mapping Library
ModelMapper, is an object-to-object framework that converts Java Beans (Pojos) from one representation to another. It automates different object mappings with a "convention follows configuration" approach allowing at the same time advanced functionality for cases with special needs. It is similar (but not a direct port) to the AutoMapper library for .NET.
ModelMapper can map a Java object on another with much more compact code and zero configuration in the simpler cases. It supports:
- Name based mapping of object properties
- Copying of public, protected and private fields
- Skipping certain fields
- Converters that affect mapping (e.g. converting strings to lowercase)
- Mapping between fields of different types (e.g. from a string to a number)
- Mapping using conditions
- Loose mapping strategy when the default is not enough
- Validation of the mapping process so that all fields are accounted for
- Complete custom control of the mapping process for special cases
- Integration with Guice or Spring
Transforming objects from one representation to another is a common pattern in Enterprise applications. An example would be domain objects that are loaded from the database and must be shown to the user in the GUI. An object in its original database format will contain a lot of properties for its lifecycle, while the user might only be interested in one or two fields at each screen. So several times the Pojo used in the database (the JPA entity) is a bit different from the Pojo used in the GUI. This is the problem that ModelMapper attempts to solve. In general object transformations are observed when information changes layers inside an Enterprise application.
Other scenarios that involve object transformations are:
- Aggregating multiple objects into one
- Computing some additional metadata on an existing object
- Transforming objects for sending to external systems
- Filling default values on undefined properties
- Converting the existing properties in some way (mapping an object to itself)
ModelMapper is available in Maven Central. For more information see the user guide and Javadoc. The source code is on GitHub.
How does this compare with other alternatives?
by
goo osi
Re: How does this compare with other alternatives?
by
Jonathan Halterman
modelmapper.org/getting-started/
Dozer requires XML configuration for this and BeanUtils only converts objects whose property names match exactly. For use cases that require specific mappings between properties, ModelMapper provides a mapping API that uses actual code rather than XML or String references, making the mappings refactoring safe and type-safe:
modelmapper.org/user-manual/property-mapping/
nice
by
Petar Shomov
Interesting and useful
by
Fred Amiter
D dest = map();
dest.setName(source.getFirstName());
Educational Content
Writing Usable APIs in Practice
Giovanni Asproni May 19, 2013
Concurrency in Clojure
Stuart Halloway May 17, 2013




Hello stranger!
You need to Register an InfoQ account or Login to post comments. But there's so much more behind being registered.Get the most out of the InfoQ experience.
Tell us what you think