Skip Headers
Oracle® Application Server TopLink Mapping Workbench User's Guide
10g Release 2 (10.1.2)
Part No. B15900-01
  Go To Documentation Library
Home
Go To Product List
Solution Area
Go To Table Of Contents
Contents
Go To Index
Index

Previous
Previous
Next
Next
 

Working with Many-to-Many Mappings

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 6-25 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.


Note:

See "Working with a 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.

The following figures illustrate a many-to-many relationship in both Java and a relational database.

Figure 6-25 Many-to-many Relationships

Description of mmmapfig.gif follows
Description of the illustration mmmapfig.gif

Creating many-to-many Mappings

Use this procedure to create a many-to-many mapping.

To create a many-to-many mapping:

  1. In the Navigator pane, select the attribute to be mapped.

  2. Many to many mapping button.
    Description of the illustration mmmapbtn.gif

    Click the Many-to-Many Mapping button on the mapping toolbar.

    Figure 6-26 Many-to-Many Mapping General Properties

    Description of mmmaptab.gif follows
    Description of the illustration mmmaptab.gif

  3. Use the Reference Descriptor drop-down list to choose the reference descriptor for this mapping.

  4. Use the Relation Table drop-down list to choose the relation table.

  5. Use the Maintain Bidirectional Relationship option to select a Relationship Partner for this mapping. See "Maintaining Bidirectional Relationships" on page 4-73 for more information.

  6. Modify any other properties, as needed. See "Working with Common Mapping Properties" on page 4-70 for more information.

  7. Click the Collection Options tab to specify how the source descriptor relates to the relation table. See "Specifying Collection Properties" on page 4-73 for more information.

  8. Click the Source Reference tab to specify how the source descriptor relates to the relation table.

    Figure 6-27 Many-to-Many Mapping Source Reference Properties

    Description of mmsource.gif follows
    Description of the illustration mmsource.gif

  9. Use the Table Reference drop-down list to choose a reference whose foreign key is in the relation table and that points to a table associated to the source descriptor. See "Creating Table References" on page 3-6 for more information.

  10. Click the Target Reference tab to specify how the reference descriptor relates to the relation table.

  11. Choose a reference whose foreign key is in the relation table and that points to a table associated to the reference descriptor. See "Creating Table References" on page 3-6 for more information.

Specifying Advanced Features by Amending the Descriptor

OracleAS TopLink can populate a collection in ascending or descending order, upon your specification. To do this, specify and write an amendment method, sending the addAscendingOrdering() or addDescendingOrdering() to the many-to-many mapping. Both messages expect a string as a parameter, which indicates what attribute from the target object is used for the ordering. This string can be an attribute name or query key from the target's descriptor. Query keys are automatically created for and with the same name as all attributes mapped as direct-to-field, type conversion, object type, and serialized object mappings.

Example 6-6 Descriptor Amendment Example

The following code example illustrates returning an Employee's projects in ascending order, according to their descriptions:

public static void addToDescriptor(Descriptor descriptor)
{
//Find the Many-to-Many mapping for the projects attribute
ManyToManyMapping projectsMapping=(ManyToManyMapping) descriptor.getMappingForAttributeName(ÒprojectsÓ);
projectsMapping.addAscendingOrdering(ÒdescriptionÓ);
}