any tool for java object to object mapping? [closed]
Asked Answered
U

9

250

I am trying to convert DO to DTO using java and looking for automated tool before start writing my own. I just wanted to know if there any free tool available for the same.

Underfur answered 16/9, 2009 at 12:50 Comment(3)
Jackson (the JSON library) has a method new ObjectMapper().convertValue(...). Jackson also has support for annotations and annotation-mixins so I find it a very good option.Deuced
Here is an article with different frameworks and performance comparison. baeldung.com/java-performance-mapping-frameworksExceeding
I readed all answers from this article, and decided to use MapStruct :)Burbage
C
109

You could try Dozer.

Dozer is a Java Bean to Java Bean mapper that recursively copies data from one object to another. Typically, these Java Beans will be of 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.

Crystalcrystalline answered 16/9, 2009 at 12:52 Comment(3)
A very very long time after, I come to make an update, I hope my comment will be read : Now Dozer seems to be a dead, and a good option is to use MapStruct, it's easy (support annotations), fast (no reflection) and seems secure (at the moment). This is included in the @Pascal Thivent list although, but I advise it (even if I had some problems by using it along with lombok).Consubstantial
Dozer seems revived with version 6: mvnrepository.com/artifact/com.github.dozermapper/dozer-coreLanguishment
The project is currently not active and will more than likely be deprecated in the future. If you are looking to use Dozer on a greenfield project, we would discourage that. If you have been using Dozer for a while, we would suggest you start to think about migrating onto another library, such as: mapstruct, modelmapperBurbage
N
413

There are some libraries around there:

  • Commons-BeanUtils: ConvertUtils -> Utility methods for converting String scalar values to objects of the specified Class, String arrays to arrays of the specified Class.

  • Commons-Lang: ArrayUtils -> Operations on arrays, primitive arrays (like int[]) and primitive wrapper arrays (like Integer[]).

  • Spring framework: Spring has an excellent support for PropertyEditors, that can also be used to transform Objects to/from Strings.

  • Dozer: Dozer is a powerful, yet simple Java Bean to Java Bean mapper that recursively copies data from one object to another. Typically, these Java Beans will be of different complex types.

  • ModelMapper: ModelMapper is an intelligent object mapping framework that automatically maps objects to each other. It uses a convention based approach to map objects while providing a simple refactoring safe API for handling specific use cases.

  • MapStruct: MapStruct is a compile-time code generator for bean mappings, resulting in fast (no usage of reflection or similar), dependency-less and type-safe mapping code at runtime.

  • Orika: Orika uses byte code generation to create fast mappers with minimal overhead.

  • Selma: Compile-time code-generator for mappings

  • JMapper: Bean mapper generation using Annotation, XML or API (seems dead, last updated 2 years ago)

  • Smooks: The Smooks JavaBean Cartridge allows you to create and populate Java objects from your message data (i.e. bind data to) (suggested by superfilin in comments). (No longer under active development)

  • Commons-Convert: Commons-Convert aims to provide a single library dedicated to the task of converting an object of one type to another. The first stage will focus on Object to String and String to Object conversions. (seems dead, last update 2010)

  • Transmorph: Transmorph is a free java library used to convert a Java object of one type into an object of another type (with another signature, possibly parameterized). (seems dead, last update 2013)

  • EZMorph: EZMorph is simple java library for transforming an Object to another Object. It supports transformations for primitives and Objects, for multidimensional arrays and transformations with DynaBeans (seems dead, last updated 2008)

  • Morph: Morph is a Java framework that eases the internal interoperability of an application. As information flows through an application, it undergoes multiple transformations. Morph provides a standard way to implement these transformations. (seems dead, last update 2008)

  • Lorentz: Lorentz is a generic object-to-object conversion framework. It provides a simple API to convert a Java objects of one type into an object of another type. (seems dead)

  • OTOM: With OTOM, you can copy any data from any object to any other object. The possibilities are endless. Welcome to "Autumn". (seems dead)

Nicknack answered 16/9, 2009 at 13:27 Comment(12)
none of the list supports annotation :(Underfur
Commons-Convert seems not just dormant but completely gone now ...President
Actually, if you want to add Moo to your list, then I can remove my answer, since your answer is clearly far more complete. ;)Roach
@RN. Dozer supports annotation since version 5.3.2 (it didn't when you wrote your comment, but nice to know for people who are looking up this answer).Spoilsport
One more options is jtransfo.org. this allows you to define the mapping using annotations on the TO. It has the additional feature of allow optional conversion of fields based on tags which are passed when doing the conversion.Wensleydale
Thanks. But it would be even more useful if this answer was split into multiple single-framework answers. In that case votes would show their actual popularity.Tollmann
Another option for Java 8+: erchu.github.io/bean-cp (disclaimer: I'm the author of this project)Haystack
Wouldn't consider Smooks since it's itroducing more problems. As i can see it's not developing, also it has no support of 3rd party frameworks (like JodaTime). Exception handling is awfull and doesn't provide meaningfull explanations. It's almost impossible to find even simple solutions for your problems (looks like developer don't use it much)Aikens
Another one to add to the list github.com/amgohan/zebra : DIY objects mapping and use zebra for unified way to inject mappers. for the sake of performance, you do objects mapping manually and don't know how organize your mappers. Zebra come with a simple and unified way to : - create your mappers - manage one way and reverse mapping in one class - register your mappers and reuse them anywhere in your application - manage deep mappingHolzer
Another option is jTransfo (jtransfo.org). jTransfo uses annotations on the DTO objects. It has good integration with Spring Framework, CDI and Hibernate. It also has some specific features which can help for security. See also: parleys.com/tutorial/…Wensleydale
@TmTron: Done and I edited the answer to remove the strike and reference the repo URL (since the main page still references 0.7.5)Morrow
You can also use Bull github.com/HotelsDotCom/bullHaugh
C
109

You could try Dozer.

Dozer is a Java Bean to Java Bean mapper that recursively copies data from one object to another. Typically, these Java Beans will be of 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.

Crystalcrystalline answered 16/9, 2009 at 12:52 Comment(3)
A very very long time after, I come to make an update, I hope my comment will be read : Now Dozer seems to be a dead, and a good option is to use MapStruct, it's easy (support annotations), fast (no reflection) and seems secure (at the moment). This is included in the @Pascal Thivent list although, but I advise it (even if I had some problems by using it along with lombok).Consubstantial
Dozer seems revived with version 6: mvnrepository.com/artifact/com.github.dozermapper/dozer-coreLanguishment
The project is currently not active and will more than likely be deprecated in the future. If you are looking to use Dozer on a greenfield project, we would discourage that. If you have been using Dozer for a while, we would suggest you start to think about migrating onto another library, such as: mapstruct, modelmapperBurbage
E
32

Another one is Orika - https://github.com/orika-mapper/orika

Orika is a Java Bean mapping framework that recursively copies (among other capabilities) data from one object to another. It can be very useful when developing multi-layered applications.

Orika focuses on automating as much as possible, while providing customization through configuration and extension where needed.

Orika enables the developer to :

  • Map complex and deeply structured objects
  • "Flatten" or "Expand" objects by mapping nested properties to top-level properties, and vice versa
  • Create mappers on-the-fly, and apply customizations to control some or all of the mapping
  • Create converters for complete control over the mapping of a specific set of objects anywhere in the object graph--by type, or even by specific property name
  • Handle proxies or enhanced objects (like those of Hibernate, or the various mock frameworks)
  • Apply bi-directional mapping with one configuration
  • Map to instances of an appropriate concrete class for a target abstract class or interface
  • Handle reverse mappings
  • Handle complex conventions beyond JavaBean specs.

Orika uses byte code generation to create fast mappers with minimal overhead.

Essayist answered 15/6, 2011 at 10:50 Comment(3)
you could provide a few words about orika - what makes it different from the othersSimper
It's probably much faster than Dozer - dozer is quiet slow.Considerable
It seems Orika is discontinued.Burbage
S
24

My ModelMapper is another library worth checking out. ModelMapper's design is different from other libraries in that it:

  • Automatically maps object models by intelligently matching source and destination properties
  • Provides a refactoring safe mapping API that uses actual code to map fields and methods rather than using strings
  • Utilizes convention based configuration for simple handling of custom scenarios

Check out the ModelMapper site for more info:

http://modelmapper.org

Sergo answered 22/6, 2011 at 19:25 Comment(0)
B
14

Use Apache commons beanutils:

static void copyProperties(Object dest, Object orig) -Copy property values from the origin bean to the destination bean for all cases where the property names are the same.

http://commons.apache.org/proper/commons-beanutils/

Botanize answered 16/9, 2009 at 13:5 Comment(2)
yeh,but properties that are in the source bean, but do not in the destination bean are simply ignored. Thanks anyways Pablojim :) I think I have to go with Dozer since we have some property names different for DO and DTO :(Underfur
Fair enough - I didn't know this... Pascal's list was pretty impressive!Botanize
G
6

There is one more Java mapping engine/framework Nomin: http://nomin.sourceforge.net.

Grate answered 9/12, 2010 at 22:34 Comment(0)
V
3

I suggest you try JMapper Framework.

It is a Java bean to Java bean mapper, allows you to perform the passage of data dynamically with annotations and / or XML.

With JMapper you can:

  • Create and enrich target objects
  • Apply a specific logic to the mapping
  • Automatically manage the XML file
  • Implement the 1 to N and N to 1 relationships
  • Implement explicit conversions
  • Apply inherited configurations
Veteran answered 4/9, 2012 at 16:13 Comment(0)
R
2

I'm happy to add Moo as an option, although clearly I'm biased towards it: http://geoffreywiseman.github.com/Moo/

It's very easy to use for simple cases, reasonable capable for more complex cases, although there are still some areas where I can imagine enhancing it for even further complexities.

Roach answered 21/4, 2011 at 15:36 Comment(0)
D
2

You can also try mapping framework based on Dozer, but with Excel mapping declaration. They've got some tools and additional cool features. Check at http://openl-tablets.sf.net/mapper

Diary answered 20/6, 2011 at 10:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.