{"id":2192,"date":"2013-12-18T13:02:03","date_gmt":"2013-12-18T11:02:03","guid":{"rendered":"https:\/\/test.viaboxx.de\/2013\/12\/18\/java-object-mapping-with-orika\/"},"modified":"2021-08-11T08:30:46","modified_gmt":"2021-08-11T08:30:46","slug":"java-object-mapping-with-orika","status":"publish","type":"post","link":"https:\/\/www.viaboxx.de\/en\/blog\/java-object-mapping-with-orika\/","title":{"rendered":"Java Object Mapping with Orika"},"content":{"rendered":"\n<p>In a layered application, you sometimes have similar class models for the same domain entities. One model could be mapped to a database (annotated for persistence), another model could be the XML representation of a RESTful API (annotated for XML\/JSON marshaling) or generated from WSDL\/XML Schema.<\/p>\n\n\n\n<p>These class models differ in some of their instance variables, types and probably have more or less similar relationships among each other.<\/p>\n\n\n\n<p>If you have such different models, you often need an efficient bidirectional mapping\/conversion of objects. Writing conversion code manually is error-prone and leads to a lot of code that needs to be tested and maintained.<\/p>\n\n\n\n<p>Therefore we used the project dozer (<a style=\"color: #c20b1d;\" href=\"http:\/\/dozer.sourceforge.net\/\">http:\/\/dozer.sourceforge.net\/<\/a>) for many years in our projects. Dozer is a Java bean to bean mapper that uses reflection to access the objects based on a XML configuration. Dozer is an old open-source project, available since 2005 and thus quite popular.<\/p>\n\n\n\n<p>But the drawback of Dozer is, that &#8211; because of reflection &#8211; its performance is not as good as it could be, and it does not fully support generics (although some generics-support is available). So it feels a little old-fashioned and cumbersome. We ran into some bugs with Dozer that could not be fixed easily because of its cluttered implementation and wild grown internal APIs.<\/p>\n\n\n\n<p>The good news is that several alternatives exist for object-to-object mapping, and my favorite is Orika (http:\/\/code.google.com\/p\/orika\/). Although this project is quite new (since 2012), it has a very stable code base, offers all relevant features, and is very easy to understand. Even in large projects that already use Dozer, a switch to Orika is possible within a short time. Our experiences with Orika in real projects are very good, so we think this framework is worth being well-known and mentioned.<\/p>\n\n\n\n<p>The main difference is its clear API and good performance, comparable with manually-written code. Orika is using byte code generation during runtime to access the objects under the hood.<\/p>\n\n\n\n<p>You can see some benchmarks between Dozer and Orika here:&nbsp;<a style=\"color: #c20b1d;\" href=\"http:\/\/blog.sokolenko.me\/2013\/05\/dozer-vs-orika-vs-manual.html\">http:\/\/blog.sokolenko.me\/2013\/05\/dozer-vs-orika-vs-manual.html<\/a>.<\/p>\n\n\n\n<p>While Dozer has a configuration API based on XML and a programmatic API, Orika only has a programmatic declarative mapping configuration.<\/p>\n\n\n\n<p>You can get started using Orika using the documentation (<a style=\"color: #c20b1d;\" href=\"http:\/\/orika-mapper.github.io\/orika-docs\/intro.html\">http:\/\/orika-mapper.github.io\/orika-docs\/intro.html<\/a>).<\/p>\n\n\n\n<p>You can easily configure Orika with the springframework, although there is no explicit support for it yet. Just implement an interface:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import ma.glasnost.orika.MapperFactory;\npublic interface MappingConfigurer {\n  void configure(MapperFactory factory);\n}\n<\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><b><strong>Orika and the springframework<\/strong><\/b><\/p>\n\n\n\n<p>You can create a fully configured\u00a0<strong>MapperFacade<\/strong>\u00a0based on multiple MappingConfigurers. A spring factory bean can be implemented like:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import de.viaboxx.mapper.Mapper;\nimport ma.glasnost.orika.MapperFactory;\nimport ma.glasnost.orika.impl.DefaultMapperFactory;\nimport org.springframework.beans.factory.FactoryBean;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.stereotype.Component;\npublic class MapperFactoryBean implements FactoryBean&lt;Mapper&gt; {\n&nbsp; &nbsp;&nbsp;  private final MappingConfigurer[] configurers;\n&nbsp; &nbsp;&nbsp;  @Autowired\n&nbsp;&nbsp;&nbsp;   public MapperFactoryBean(MappingConfigurer... configurers) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;     super();\n     this.configurers = configurers;\n   }\n\n   public Mapper getObject() {\n     MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();\n     for (MappingConfigurer configurer : configurers) {\n       configurer.configure(mapperFactory);\n     }\n     return new OrikaMapper(mapperFactory.getMapperFacade());\n   }\n\n   public Class&lt;?&gt; getObjectType() {\n    return Mapper.class;\n   }\n\n  public boolean isSingleton() {\n    return true;\n  }\n}\n<\/pre>\n\n\n\n<p>By the way:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Use a&nbsp;<strong>PassThroughConverter<\/strong>&nbsp;to avoid objects from being deep-copied. If the immutable instance is not supported by Orika out-of-the-box, such as a joda-time DateTime, you can register a PassThroughConverter for its class to copy it by reference.<\/li><\/ul>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Abstraction<\/strong><\/p>\n\n\n\n<p>If you want to use an abstraction to exchange the mapping framework, we suggest some interfaces, that are easy to implement with either Dozer or Orika.<\/p>\n\n\n\n<p>1. The general&nbsp;<strong>Mapper<\/strong>. It has methods to map into a new instance and an existing instance.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public interface Mapper {\n  \/**\n  * Create and return a new instance of type D mapped with the properties of\n  * &lt;code&gt;sourceObject&lt;\/code&gt;.\n&nbsp; * @param sourceObject     the object to map from\n  * @param destinationClass the type of the new object to return\n  * @return a new instance of type D mapped with the properties of\n  *         &lt;code&gt;sourceObject&lt;\/code&gt;\n  *\/\n  &lt;S, D&gt; D map(S sourceObject, Class&lt;D&gt; destinationClass);\n\n  \/**\n  * Maps the properties of &lt;code&gt;sourceObject&lt;\/code&gt; onto\n  * &lt;code&gt;destinationObject&lt;\/code&gt;.\n  * @param sourceObject      the object from which to read the properties\n  * @param destinationObject the object onto which the properties should be mapped\n  *\/\n  &lt;S, D&gt; void map(S sourceObject, D destinationObject);\n}\n<\/pre>\n\n\n\n<p>2. The&nbsp;<strong>Mapper implementation<\/strong>&nbsp;for Orika:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import ma.glasnost.orika.MapperFacade;\npublic class OrikaMapper implements Mapper {\n  private final MapperFacade mapperFacade;\n\n  public OrikaMapper(MapperFacade mapperFacade) {\n    this.mapperFacade = mapperFacade;\n  }\n\n  @Override\n  public &lt;S, D&gt; D map(S sourceObject, Class&lt;D&gt; destinationClass) {\n    return mapperFacade.map(sourceObject, destinationClass);\n  }\n\n  @Override\n  public &lt;S, D&gt; void map(S sourceObject, D destinationObject) {\n    mapperFacade.map(sourceObject, destinationObject);\n  }\n}\n<\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>CustomMappers<\/strong><\/p>\n\n\n\n<p>In real projects you probably have to write many conversion code to bridge differences between the models. We recommend to use CustomMappers to do so.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">factory.classMap(Person.class, Customer.class)\n  .field(\"firstName\", \"name\")\n  .field(\"dateBirth\", \"dateOfBirth\")\n  .customize(new CustomMapper&lt;Person, Customer&gt;() {\n    @Override\n    public void mapAtoB(Person source, Customer target, MappingContext context) {\n      \/\/ some mapping logic here...\n    }\n\n    @Override\n    public void mapBtoA( Customer source, Person target, MappingContext c) {\n      \/\/ mapping logic here...\n   }\n}).register();\n<\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Summary<\/strong><\/p>\n\n\n\n<p>If you need to map objects to objects, consider using Orika.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a layered application, you sometimes have similar class models for the same domain entities. One model could be mapped to a database (annotated for persistence), [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"default","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"default","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[76],"tags":[],"class_list":["post-2192","post","type-post","status-publish","format-standard","hentry","category-code"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Simon Tiffert","author_link":"https:\/\/www.viaboxx.de\/en\/blog\/author\/simon-tiffertviaboxx-de\/"},"uagb_comment_info":0,"uagb_excerpt":"In a layered application, you sometimes have similar class models for the same domain entities. One model could be mapped to a database (annotated for persistence), [&hellip;]","_links":{"self":[{"href":"https:\/\/www.viaboxx.de\/en\/wp-json\/wp\/v2\/posts\/2192"}],"collection":[{"href":"https:\/\/www.viaboxx.de\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.viaboxx.de\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.viaboxx.de\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.viaboxx.de\/en\/wp-json\/wp\/v2\/comments?post=2192"}],"version-history":[{"count":2,"href":"https:\/\/www.viaboxx.de\/en\/wp-json\/wp\/v2\/posts\/2192\/revisions"}],"predecessor-version":[{"id":7011,"href":"https:\/\/www.viaboxx.de\/en\/wp-json\/wp\/v2\/posts\/2192\/revisions\/7011"}],"wp:attachment":[{"href":"https:\/\/www.viaboxx.de\/en\/wp-json\/wp\/v2\/media?parent=2192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.viaboxx.de\/en\/wp-json\/wp\/v2\/categories?post=2192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.viaboxx.de\/en\/wp-json\/wp\/v2\/tags?post=2192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}