Java mapping: Selma vs MapStruct

JavaMappingMapstruct

Java Problem Overview


Currently there are two main popular Java Object to Object mapping frameworks that supersede Dozer (http://dozer.sourceforge.net/documentation/mappings.html), they are:

  1. Selma - http://www.selma-java.org/
  2. MapStruct - http://mapstruct.org/

With the exception of this page (http://vytas.io/blog/java/java-object-to-object-mapping-which-framework-to-choose-part-2/) I haven't been able to find much online regarding which framework is better than the other, or under what circumstances they are better. Wondering if anyone you can shed some light on this. In terms of functionality based on the documents, they seem to be doing the same thing.

Java Solutions


Solution 1 - Java

(Original author of MapStruct here, so naturally I am biased)

Indeed, both projects are based on the same general idea of generating mapping code at compile time; I recommend you MapStruct for the following reasons:

  • Proven and stable codebase: MapStruct is the older of the two, coming up with the idea of mapping generation originally. It has been enhanced and polished over quite a long time, based on real-world feedback from usage in many different projects; We released the stable 1.0 Final last year
  • Larger developer and user community as per the number of committers (MapStruct, Selma) and user questions (MapStruct, Selma)
  • Feature-rich (Some things supported in MapStruct I didn't find (to the same extend) in the Selma docs):
  • Many built-in type conversions, including advanced support for JAXB types such as JAXBElement
  • Support for default values and constants
  • Mapping customizations through inline expressions
  • Sharing configurations across mappers
  • Nicely integrates with CDI and JSR 330 (in addition to Spring)
  • Eclipse plug-in avaible: Still work in progress, but its quickfixes and auto-completions are already very helpful when designing mapper interfaces
  • IntelliJ plug-in: helps when editing mapper interfaces via auto-completion, go to referenced properties, refactoring support etc.

Solution 2 - Java

(Original author of Selma so slight different point of view)

Selma and MapStruct does the same job with some differences. First it appears that Selma generated code is just a bit faster than MapStruct (http://javaetmoi.com/wp-content/uploads/2015/09/2015-09-mapping-objet-objet2.png). The 0.13 release number does not really reflects the maturity of code Selma is stable and robust it is in use in production for 2 years.

The main idea behind Selma is to prohibit magic conversion and just automate all mappings without any side effects. When mapping appears to be too complex, the developer should handle it by himself using custom mappings or interceptor.

The footprint of Selma is built to be as small as possible we only depend on a JavaWriter and the JDK.

Selma tries to only use static compiled generated code without any reflection at runtime or pseudo-code written in string fields.

You can use composition to build a chain of mappers and inside a single mapper you can have global configuration that can be overwritten on a per method basis.

Compiler messages are built to give developer early feedback, tips to solve the issue and learn the API.

At the end for sure MapStruct is more feature rich but Selma gives developer all the tools needed for complex mapping with the responsibility of writing the business logic. You could also find one of the 2 APIs nicer than the other from a user perspective so best thing to do is to try both and choose the one you feel more comfortable with. It won't be time consuming.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJackDevView Question on Stackoverflow
Solution 1 - JavaGunnarView Answer on Stackoverflow
Solution 2 - JavaslemesleView Answer on Stackoverflow