# Many-to-many mapping

Use a Many-to-Many Mapping to define a many-valued association with many-to-many multiplicity. A many-to-many mapping has two sides: the owning side and non-owning side. You must specify the join table on the owning side. For bidirectional mappings, either side may be the owning side.

1. In the Persistence Outline view, select the field to map. The Persistence Properties view (for attributes) displays the properties for the selected.

2. In the Map As field, select Many-to-Many.

3. Use this table to complete the fields on the General tab of the Persistence Properties view.

   Property | Description | Default
   --- | --- | ---
   Target Entity | The entity to which this attribute is mapped. | null You do not need to explicitly specify the target entity, since it can be inferred from the type of object being referenced.

   Cascade Type

   See "Cascade Type" for details.

   - Default

   - All

   - Persist

   - Merge

   - Remove

   Default

   Fetch Type

   Defines how data is loaded from the database. See "Fetch Type" for details.

   - Eager

   - Lazy

   Eager

   Mapped By | The database field that owns the relationship. |

   Order By

   Specify the default order for objects returned from a query. See "Order By" for details.

   - Primary key

   Primary key

4. Use this table to complete the fields on the Join Table tab tab in the Persistence Properties view.

   Property | Description | Default
   --- | --- | ---
   Name | Name of the join table that contains the foreign key column. | You must specify the join table on the owning side. By default, the name is assumed to be the primary tables associated with the entities concatenated with an underscore.
   Join Columns | Select Override Default, then Add, Edit, or Remove the join columns. | By default, the name is assumed to be the primary tables associated with the entities concatenated with an underscore.
   Inverse Join Columns | Select Override Default, then Add, Edit, or Remove the join columns. | By default, the mapping is assumed to have a single join.

5. To add a new Join or Inverse Join Column, click Add.

   To edit an existing Join or Inverse Join Column, select the field to and click Edit.

Eclipse adds the following annotations to the field:

    @JoinTable(joinColumns=@JoinColumn(name="<JOIN_COLUMN>"),
        name = "<JOIN_TABLE_NAME>")
    @ManyToMany(cascade=CascadeType.<CASCADE_TYPE>, fetch=FetchType.<FETCH_TYPE>,
        targetEntity=<TARGET_ENTITY>, mappedBy = "<MAPPED_BY>")
    @OrderBy("<ORDER_BY>")

Mapping an entity

Persistence Outline view
Persistence Properties view (for attributes)

Understanding OR mappings
Understanding JSR220: EJB 3.0