views:

686

answers:

1

This is a problem I keep on running into:

I would like to have hibernate manage a single table that represents a collection of collections. For example:

  • a Map of Maps
  • List of Sets
  • Map of Lists

Example, I would like to be able to represent this:

class OwningClass {  
    Long entityId;  
    Map<String, List<Element>> mapOfLists;
}

class Element {
    String data_1;
    boolean data_2;
}

as a single table:

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)

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.

  • Should I extend AbstractPersistentCollection?
  • CompositeUserType?

Its possible to manage if multiple tables are o.k. but obviously that is lame from the db perspective.

+3  A: 

Found the answer elsewhere on the site :-) http://stackoverflow.com/questions/475879/multimap-in-hibernate/475921#475921

which points to: http://blog.xebia.com/2007/10/05/mapping-multimaps-with-hibernate/

Haven't investigated the solution in the blog post yet but looks like it is answering exactly my issue.

Pat
+1 for answering your own question and providing that very useful link!
javashlook