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 JPA Structure view, select the field to map. The JPA Details 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 General information fields of the JPA Details view.

    Property Description Default
    Map As Defines this mapping as a Many to Many Mapping.

    This field corresponds to the @ManyToMany annotation.

    Many to Many
    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.

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

    • Lazy

    Lazy
    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.
    • No ordering

    • Primary key

    • Custom

    No ordering

  4. Use this table to complete the fields in the Join Table Information area in the JPA Details 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>")


 

Related task

Mapping an entity

 

Related reference

JPA Structure view
JPA Details view (for attributes)

 

Related concept

Understanding OR mappings
Understanding EJB 3.0 Java Persistence API