4.6. Many-to-Many

The @javax.persistence.ManyToMany annotation defines a many-valued association with many-to-many multiplicity. In Naked Objects' terms this means two entities each referencing a collection of the other.

As you may well know, many-to-many associations in a relational database always require a link (or associative table), consisting of the primary keys of each of the entities. Note the similarity to the mapping of a unidirectional @OneToMany association (see Section 4.5, “One-to-Many”), which in this light can be thought of as a special case of @ManyToMany. Indeed the same annotation elements for the @OneToMany annotation apply to the c annotation.

Every many-to-many association has two sides, the owning side and the non-owning, or inverse, side. The join table is specified on the owning side. If the association is bidirectional, either side may be designated as the owning side. If the Collection is defined using generics to specify the element type, the associated entity class does not need to be specified; otherwise it must be specified.