matthieus
matthieus

Reputation: 752

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.

Upvotes: 6

Views: 1305

Answers (1)

Pascal Thivent
Pascal Thivent

Reputation: 570515

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

Upvotes: 6

Related Questions