Tools

Posted By Mariusz Wyszomierski

In multi-tier applications there is often a need for mapping between two data models. In this article you can read about comparing two mapping libraries:

Dozer (http://dozer.sourceforge.net/) – popular library, where mapping is configured by XML files

MapStruct (http://mapstruct.org/) – library, which is used for example in JHipster generator

The most common use case is coping data from entity objects to DTO objects usually used in service REST/SOAP.

Dozer MapStruct Description

Mapping rules are defined in XML files, which are then loaded during application runtime .

Dozer is using reflection mechanism for mapping. Mapping rules are defined in Java files, mostly in annotations. Based on this rules MapStruct generates Java classes, which are then used for mapping. Speed Dozer uses reflection mechanism for mapping, which means it is slower than MapStruct.

In example (link) it is mapped 1 000 000 objects.

On my notebook it takes about 16 seconds. MapStruct invoke methods of classes, which were generated before compilation.

Thanks to that it works faster than Dozer.

In example (link) it is mapped 1 000 000 objects.

On my notebook it takes about 3 seconds. MapStruct is about 500% faster than Dozer. Security

When you make a mistake in XML that contains mapping rules you will know about it only during invoking mapping (runtime error). When you make a mistake in property name, you will know about it during compilation. Thanks that application will be more reliable . Immutable objects Dozer can access private properties (by reflection). It makes possible to map immutable objects which haven’t setters. It requires that immutable object has a constructor without parameters and in mapping files you should add attribute is-accessible="true" to destination fields. Unfortunately MapStruct hasn’t ability to map immutable objects.

There is an open issue about that. Debugging Mapping rules are defined mostly in XML files. During application runtime that files are loaded. Thus debugging mapping process is difficult – you have to set breakpoints in Dozer classes. You can set breakpoint in classes, which was generated during compilation. Thanks that you can easily determine which classes are used for mapping and debug them. Popularity

( as at the date 23.03.2016)

Creation date: 04.06.2005

Stackoverflow: 1610 postów

Github: https://github.com/DozerMapper/dozer

Last release date: 22.04.2014 Creation date: 22.05.2012

Stackoverflow: 108 postów

Github: https://github.com/mapstruct/mapstruct

Last release date: 16.03.2016 Additional advantages More default mapping for example: Joda-Time, JAXBElement Mapping code only in Java files – faster determination , which code is responsible for data mapping . Event handling ( @BeforeMapping, @AfterMapping ) Built-in support for decorators

I’ve prepared a small application, which does the same mapping using Dozer and MapStruct libraries. You can easily notice the difference in speed.

https://github.com/speedlog/mapping-tools-performance