title: Hibernate: Collections of Collections
body: <p>This is a problem I keep on running into:</p>
  
  <p>I would like to have hibernate manage a single table that represents a collection of collections. For example:</p>
  
  <ul>
  <li>a Map of Maps</li>
  <li>List of Sets</li>
  <li>Map of Lists</li>
  </ul>
  
  <p>Example, I would like to be able to represent this:</p>
  
  <pre>
  class OwningClass {  
      Long entityId;  
      Map&lt;String, List&lt;Element>> mapOfLists;
  }
  
  class Element {
      String data_1;
      boolean data_2;
  }
  </pre>
  
  <p>as a single table:</p>
  
  <pre>
  OWNER (Foreign key to the owner of this element) 
  MAP_KEY (varchar(30) )
  LIST_INDEX (int)
  ELEMENT_DATA_1 (varchar(1020)
  ELEMENT_DATA_2 (bit)
  </pre>
  
  <p>It doesn't seem possible without a custom hibernate code, which I don't mind. But I was hoping someone had some guidance on what that custom code should look like.</p>
  
  <ul>
  <li>Should I extend AbstractPersistentCollection?</li>
  <li>CompositeUserType?</li>
  </ul>
  
  <p>Its possible to manage if multiple tables are o.k. but obviously that is lame from the db perspective.</p>