title: Complex Java bean mapping
body: <p>I need to find a solution to map entities to DTOs.</p>
  
  <p>I know there is a lot of libraries for that but the conversion I need to do is quite complex and I have not been able to find a satisfactory solution.</p>
  
  <p>The entities I have look like this:</p>
  
  <pre><code>public class Source {
      private String name;
      private String desc;
      private Set&lt;Details&gt; details = new HashSet&lt;Details&gt;();
  }
  
  public class Details {
      private String code;
      private String type;
      private Set&lt;Attribute&gt; attributes = new HashSet&lt;Attribute&gt;();
  }
  
  public class Attribute {
      private String name;
      private String value;
  }
  </code></pre>
  
  <p>For the sake of clarity, I removed persistence annotations and getters/setters and omitted some fields.</p>
  
  <p>And the DTOs:</p>
  
  <pre><code>public class DestDTO {
      private String name;
      private String desc;
      private List&lt;GenericDetailsDTO&gt; genericDetails;
  }
  
  public class GenericDetailsDTO {
      private String code;
      private List&lt;DetailsDTO&gt; details;
  }
  
  public class DetailsDTO {
      private String type;
      private String field1;
      private Boolean field2;
      private Integer field3;
  }
  </code></pre>
  
  <p>The mapping I want to do is as follow:</p>
  
  <ul>
  <li>Source.name => DestDTO.name and Source.desc => DestDTO.desc, okay easy.</li>
  <li>Group Details instances in GenericDetailsDTO instances according to their code. For example, Details instances having "a" as code are all gathered into a single GenericDetailsDTO instance having "a" as code.</li>
  <li>Each instance of Details is then mapped as item of the list details of the appropriate GenericDetailsDTO instance. This mapping is also complex:
  <ul>
  <li>Details.type => DetailsDTO.type, okay easy also.</li>
  <li>For each "Attribute" of the attributes set of the Details instance, map the "value" field, with the correct type (String, Boolean or whatever), to the field of the DetailsDTO instance having as name the value of the field "name" of the same "Attribute" instance.</li>
  </ul></li>
  </ul>
  
  <p>I tried JMapper but I have not been able to implement such a solution.</p>
  
  <p>I have been able to do it by using Dozer but I would like to avoid this library because of its slow performances (According to various articles on the net such as <a href="http://www.christianschenk.org/blog/java-bean-mapper-performance-tests/" rel="nofollow">http://www.christianschenk.org/blog/java-bean-mapper-performance-tests/</a>).</p>
  
  <p>In a perfect world, I would also like to use as much as possible annotation/api configuration and avoid XML configuration. And if the mapper instance could also be injected as bean through Spring API (Not XML once again), it would really be a wonderful world.</p>
  
  <p>So my question is: would you have any advises or could you please recommend me a library for this issue?</p>
  
  <p>Thanks for your help.</p>
  
  <p>PS: Sorry if it is not very clear, but it is quite hard to explain in few words. Please, do not hesitate to ask more explanation.</p>