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.
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 List
s, Map
of Set
s, 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>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With