Skip Headers

# Many-to-Many Mapping

Previous

Next

Many-to-many mappings represent the relationships between a collection of source objects and a collection of target objects. They require the creation of an intermediate table for managing the associations between the source and target records.

Figure: Many-to-many Relationships illustrates a many-to-many mapping in Java and in relational database tables.

Many-to-many Relationships

Description of "Many-to-many Relationships"

Many-to-many mappings are implemented using a relation table. This table contains columns for the primary keys of the source and target tables. Composite primary keys require a column for each field of the composite key. The intermediate table must be created in the database before using the many-to-many mapping.

The target class does not have to implement any behavior for the many-to-many mappings. If the target class also creates a many-to-many mapping back to its source, then it can use the same relation table, but one of the mappings must be set to read-only. If both mappings write to the table, they can cause collisions.

Note: See "Configuring Container Policy" for information on using `Collection` classes other than `Vector` with one-to-many mappings.

Indirection is enabled by default in a many-to-many mapping, which requires that the attribute have the `ValueHolderInterface` type or transparent collections.

You can use a many-to-many mapping with a change policy (see "Configuring Change Policy".

See "Configuring a Relational Many-to-Many Mapping" for more information.