Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA2 persistence for a @ManyToMany Map containing a Set

I need to persist a member with the type Map<Item, Set<Item>>, using JPA2 annotations. The relation is many to many and Item objects are entities.

Should I create a separate intermediary Entity holding Set<Item> or is a direct mapping possible?

Please advise if some context is missing.

like image 233
matthieus Avatar asked Nov 08 '10 15:11

matthieus


1 Answers

Should I create a separate intermediary Entity holding Set<Item> or is a direct mapping possible?

JPA doesn't support nested collection relationships (List of Lists, Map of Sets, etc). Here is the relevant section of the specification about Map:

2.7 Map Collections

Collections of elements and entity relationships can be represented as java.util.Map collections.

The map key and the map value independently can each be a basic type, an embeddable class, or an entity.

...

So yes, use an entity holding the Set<Item> and then map your relation as Map<Item, MyHolder>.

References

  • JPA 2.0 specification
    • Section 2.7 "Map Collections"
  • JPA Wiki Book
    • Nested Collections, Maps and Matrices
like image 179
Pascal Thivent Avatar answered Sep 28 '22 03:09

Pascal Thivent