60

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.

Gunnar
  • 15,675
  • 1
  • 44
  • 63
JackDev
  • 10,244
  • 11
  • 45
  • 66
  • Related list of mappers: https://stackoverflow.com/questions/1432764/any-tool-for-java-object-to-object-mapping – tkruse Dec 12 '17 at 05:04

2 Answers2

88

(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):
  • 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.
Gunnar
  • 15,675
  • 1
  • 44
  • 63
  • Hello Gunner, Thanks for the all details around MapStruct. We're using Dozer 5.4.0 and thinking of switching now as it has been performing worst. Do you think MapStruct supports concepts like like dozer "one-way", is-accessible="true", "a-deep-index-hint" attribute and specifying custom-convertors? I would love to try out MapStruct. – Yogendra Jun 19 '16 at 06:14
  • I recommend you take a look at the [reference documentation](http://mapstruct.org/documentation/). I don't know the Dozer terms/features, but suppose "one-way" is supported implicitly by the fact that you declare specific mapping methods in MapStruct. Custom converters are represented by hand-written mapping methods. Not sure about the other two, if you can point me to a description of them, I may be point you to the counterpart in MapStruct. – Gunnar Jun 20 '16 at 06:58
  • I use MapStruct in my project, with the latest version it's fast and I had 0 issues. I can do what I want easily, and also, introduction of the ObjectFactories make it easy to do some pre-mapping processing. (like accessing database, or whatever you want). But I wouldn't compare Selma because I didn't tried it yet, only giving feedback on MapStruct ;) – Alex Jul 23 '18 at 09:19
88

(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.

kenn3th
  • 1,036
  • 2
  • 17
  • 37
slemesle
  • 1,009
  • 7
  • 5
  • 5
    Providing picture without any information about testing stand and benchmark itself is just a speculation. Here is more representative source: https://github.com/arey/java-object-mapper-benchmark – Maxim Kolesnikov Sep 21 '16 at 20:59
  • 1
    Does anyone tries to see why Selma is faster than Mapstruct as they are both using compile generated mappers. – gabuzo Nov 21 '16 at 23:39
  • 48
    Two original authors discussing. I do love Stackoverflow. – weberjn Mar 22 '17 at 12:14
  • 3
    Just a short update to share the availability of Selma 1.0, It now handles all feature I expected in it from the beginnings. – slemesle May 01 '17 at 21:41
  • Don't use this. The compile error message is very confusing and ambiguous. – fangdi May 16 '18 at 09:22
  • Selma, love the speed and the lower amount of dependencies, but it would help to see some videos demoing the capabilities. It would be a huge win if you had a video addressing large nested object mappings that have only 50% similarity. I know this is a hard problem no matter the approach. – cody.tv.weber Jan 07 '20 at 19:38