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 5-17 illustrates a many-to-many mapping in Java and in relational database tables.
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.
Indirection is enabled by default in a many-to-many mapping, which requires that the attribute have the ValueHolderInterface type or transparent collections.
The following figures illustrate a many-to-many relationship in both Java and a relational database.
Figure 5-17 Many-to-many Relationships
Copyright © 1997, 2004, Oracle. All rights reserved.