title: Using JPA/Hibernate Map for rows/columns values
body: <p>I'm using Hibernate 3.6 but would prefer to stick to JPA annotations if possible.</p>
  
  <p>I have a parent entity which has an id, and possibly other simple properties (name, description,etc) but will have a 2-D collection of children (in the simple case, Strings). </p>
  
  <p>Ideally I'd like the ParentEntity to look like this: </p>
  
  <pre><code>class ParentEntity {
       @Id
       private Long id;
  ....
       @????
       // first Long should be column_index, second Long should be row_index
       private Map&lt;Long,Map&lt;Long,String&gt;&gt; gridOfStrings;
  }
  </code></pre>
  
  <p>In my database, my gridOfString is represented like this:</p>
  
  <pre><code>GridItems
  ------------
  grid_id (int,FK to grid)
  column_index (int)
  row_index (int)
  value (varchar)
  </code></pre>
  
  <p>I have some amount of flexibility to change the schema or the way that the entity works. </p>
  
  <p>What's the clearest way to have Hibernate do the magic for me? I want to avoid having to write the logic for the mapping/transformation of a list of entities into columns of rows of strings.</p>
  
  <p><strong>Alternative approach</strong> </p>
  
  <p>I can assume that I'll only have 2 or 3 columns and make them separate Maps. I'm not sure how to have hibernate filter each join though.</p>
  
  <p>The Entity could look like this:</p>
  
  <pre><code>@???
  //rows where column_index=1
  private Map&lt;Long,String&gt; column1;
  ...
  private Map&lt;Long,String&gt; column2;
  ...
  private Map&lt;Long,String&gt; column3;
  </code></pre>