Java object to object mapping - which framework to choose? - Part 1

Many applications in my career had a need for a custom POJO mapper. Because of project complexity (and absence of existing mapping frameworks at that time) usually manual mapping was used in almost 99% cases :) What about another 1%? No mapping needed at all :)

I was following the discussions and mapping frameworks from time to time and was trying to figure out if I need one already? Are there any frameworks to do the job? Are there any frameworks to do the job right? Is it worth to look for such framework?

Photographs by Louis K..

It depends.

  • If your task is simple - then why another framework in your classpath?
  • If your objects are complex - will the framework do the job? You know the logic behind, so it might happen that you will complete the task faster than finding, configuring, running and testing a new framework;
  • For one-to-one mapping and cloning (same source type, same target type) maybe apache commons beanutils is enough?
  • Boring daily mapping? Yeah, maybe the mapper will help to make your life more interesting!

What are the requirements?

Let's make some requirements for a mapping framework. These will eliminate many of already existing in the market.

  • DTO to JPA (or other DTO, does not matter too much) - meaning that JPA entity will not be exposed to a public world;
  • Compile time. Why? Do you really want a runtime bean resolving, reflection, etc? Then think about performance before choosing the right framework;
  • Annotation based. Ok, annotation based and compile time requirements are little bit too much, because annotation is used only to generate a code for you. But if it is easy to use - why not?

Mapping frameworks

There is a great article covering the existing frameworks: Java Object to Object Mapper

The majority of these frameworks do not have annotation based configuration except Dozer. However Dozer does not fly well according the performance comparision Dozer vs Orika vs Manual. So I excluded this framework as well.

More interesting solutions are Selma and MapStruct.

I will try to cover them in Part 2!

Photographs by Louis K..

vytas

Senior software engineer, software architect for java enterprise based solutions