title: Approaches for populating Java objects from maps
body: <p>I'm dealing with a recurring pattern at work.</p>
  
  <pre><code>public class SapExample
  {
  
    public static void main(String[] args)
    {
      // read SAP data..
  
      // get table
      List&lt;Map&lt;String, Object&gt;&gt; objects = getTable("ET_WIDGET");
  
      for (Map&lt;String, Object&gt; entry : objects)
      {
        Object value = entry.get("WDGT_ID");
        String id = (value != null) ? String.valueOf(value) : ""; // or null
  
        value = entry.get("WDGT_NAME");
        String name = (value != null) ? String.valueOf(value) : ""; // or null
  
        value = entry.get("WDGT_DESC");
        String description = (value != null) ? String.valueOf(value) : ""; // or null
  
        // create object from data
        //Widget obj = new Widget(id, name, description);
      }
    }
  
    private static List&lt;Map&lt;String, Object&gt;&gt; getTable(String string)
    {
      return Collections.emptyList();
    }
  }
  </code></pre>
  
  <p><sub>Easy example with no relations between objects. More complex scenarios, where class A has a member of class B do also occur and are solved with use of foreign keys.</sub></p>
  
  <p>Tables returned from the database
  are handled as <code>List&lt;Map&lt;String, Object&gt;&gt;</code>, where <code>Map&lt;String, Object&gt;</code> depicts a row in given table that maps the column names to values of type <code>Object</code>, which internally mostly are of type <code>String</code> or <code>Date</code>. </p>
  
  <p>The map contains the data needed to initialize objects of a certain type, which depends on the current table. I <strong>can't</strong> change that.</p>
  
  <p>Programmers directly or indirectly access the data in the way I showed above and create instances of the desired objects.</p>
  
  <hr>
  
  <p>I find that a very verbose and unesthetic way of programming. </p>
  
  <p>Is it possible to utilize JAXB or other technologies to solve this problem in a declarative way (e.g. by use of XML)?
  If it is possible to use JAXB, what do I have to do? I suppose I have to override some classes.</p>