StackTips

Popular Java Object Mapping Frameworks

Applications often consist of similar but different object models, where the data in two models may be similar but the structure and concerns of the models are different.

Object mapping makes it easy to convert one model to another, allowing separate models to remain segregated. Here are some of the popular Java object mapping frameworks:

1. Dozer

Dozer is a Java Bean to Bean mapper that recursively copies data from one object to another. Typically, these Java Beans can be of same or different complex types.

Dozer supports simple property mapping, complex type mapping, bi-directional mapping, implicit-explicit mapping, as well as recursive mapping. This includes mapping collection attributes that also need mapping at the element level.

Installing Dozer

If you’re using Maven build tools, for using Dozer framework you need to add the following package dependency

com.github.dozermapperdozer-core6.4.1

If you’re using Gradle build system, you can include the following package dependency in your build.gradle file.

compile group: 'com.github.dozermapper', name: 'dozer-core', version: '6.4.1'

Check out the Dozer framework documentation here for more details.

2. MapStruct

MapStruct is a code generator that greatly simplifies the implementation of mappings between Java bean types based on the annotation approach. It is a Java annotation processor for generating the type-safe bean mapping classes.

All you have to do is to define a mapper interface which declares any required mapping methods. During compilation, MapStruct will generate an implementation of this interface. This implementation uses plain Java method invocations for mapping between source and target objects, i.e. no reflection or similar.

This is probably the simplest and quickest option for writing mapping code. It saves time by generating code which is generally tedious and error-prone to write your own.

Installing MapStruct

If you’re using Maven build tools, for using MapStruct framework you need to add the following package dependency

...
1.3.0.Beta1
...

org.mapstructmapstruct-processor${org.mapstruct.version}provided

Optionally, you can add MapStruct classpath to annotation processor paths as follows.

org.apache.maven.pluginsmaven-compiler-plugin3.5.11.8
                1.8org.mapstructmapstruct-processor${org.mapstruct.version}
...

If you’re using Gradle build system, you can add the following package dependency in your build.gradle file in order to enable MapStruct.

...
plugins {
    ...
    id 'net.ltgt.apt' version '0.15'
}

dependencies {
    compile 'org.mapstruct:mapstruct-jdk8:1.2.0.Final'
    apt 'org.mapstruct:mapstruct-processor:1.2.0.Final'
}
...

Check out the MapStruct framework documentation here.

3. ModelMapper

The ModelMapper is framework aimed at simplifying the Java object mapping, by automatically determining how one object model maps to another, based on conventions.

It analyses your Java object model to intelligently determine how data should be mapped without doing any manual mapping.

The ModelMapper API is type-safe and refactoring-safe, using the actual code, rather than string references, to map properties and values.

Installing ModelMapper

If you’re using Maven build tools, for using ModelMapper framework, you need to add the following package dependency

org.modelmappermodelmapper2.2.0

If you’re using Gradle build system, you can add the following package dependency in your build.gradle file.

dependencies {
    compile group: 'org.modelmapper', name: 'modelmapper', version: '2.2.0'
}

Check out the MapStruct framework documentation here.

4. JMapper

JMapper is a Java bean to bean mapper framework based n Javassist framework. It allows you the ability to do dynamic mappings, multi-relational mappings, inherited mapping and other features without compromising on performance.

The framework allows you to map bean using annotation, XML or API-based configurations.

Installing JMapper

If you’re using Maven build tools, for using ModelMapper framework, you need to add the following package dependency

com.googlecode.jmapper-frameworkjmapper-core1.6.0

If you’re using Gradle build system, you can add the following package dependency in your build.gradle file.

dependencies {
    compile group: 'com.googlecode.jmapper-framework', name: 'jmapper-core', version: '1.6.0'
}

Check out the JMapper framework documentation here.