title: ModelMapper: Map collections to collections of other structure
body: <p>I map business objects to entities and there are cases where the structure of an entity is different from the business objects.<br>
  I have <code>userCategories</code> which are stored in the business object <code>RecipeBo</code> as strings, because the BO does not have to know anything about the internal structure of the entities. These strings need to be mapped to a relation of <code>Recipe</code> and <code>RecipeUserCategoryRel</code>, in addition to it, another field, <code>userId</code> of <code>RecipeBo</code> needs to be mapped in this <code>RecipeUserCategoryRel</code> too.</p>
  
  <p>My approach (which works) is to create a wrapper and manually create the relations by hand, but this looks like tinkering:</p>
  
  <pre><code>public class BoMapper
  {
      private final static ModelMapper modelMapper = new ModelMapper();
  
      static
      {
          modelMapper.addMappings(new IngredientMap());
      }
  
      public static void map(Object from, Object to)
      {
          modelMapper.map(from, to);
  
          if (from instanceof RecipeBo &amp;&amp; to instanceof Recipe)
          {
              RecipeBo recipeBo = (RecipeBo)from;
              List&lt;String&gt; userCategories = recipeBo.getUserCategories();
              List&lt;RecipeUserCategoryRel&gt; recipeUserCategoryRels = new ArrayList&lt;&gt;();
  
              for (String userCategory : userCategories)
              {
                  recipeUserCategoryRels.add(new RecipeUserCategoryRel(userCategory, recipeBo.getUserId()));
              }
  
              Recipe recipe = (Recipe)to;
  
              recipe.setRecipeUserCategoryRels(recipeUserCategoryRels);
          }
      }
  }
  </code></pre>
  
  <p>Is there a better approach of that what I'm doing in BoMapper, e.g. using converters or something? The difficulty is to map each element of the list and add the <code>userId</code> field too.</p>