Home / Middleware / Oracle Fusion Middleware Online Documentation Library, 11g Release 1 (11.1.1.3.0) / Developer Guides
Fusion Middleware Developer's Guide for Oracle TopLink
ContentsOpens a new window
Opens a new window
Page 52 of 158

34 Configuring a Relational Many-to-Many Mapping

This chapter describes the various components that you must configure in order to use a relational many-to-many mapping.

This chapter includes the following sections:

For information on how to configure TopLink mappings options common to two or more mapping types, see Chapter 121, "Configuring a Mapping"Opens a new window.

For information on how to create TopLink mappings, see Chapter 120, "Creating a Mapping"Opens a new window.

Table 34-1Opens a new window lists the configurable options for a relational many-to-many mapping.

34.1 Introduction to Relational Many-to-Many Mapping Configuration

Table 34-1Opens a new window lists the configurable options for a relational many-to-many mapping.

Table 34-1 Configurable Options for Relational Many-to-Many Mapping

Option to Configure Oracle JDeveloper
TopLink Workbench
Java

Reference descriptor (see Section 28.4, "Configuring Reference Descriptor"Opens a new window)

Supported Supported Supported

Method or direct field access (see Section 121.6, "Configuring Method or Direct Field Accessing at the Mapping Level"Opens a new window)

Supported Supported Supported

Read-only mapping (see Section 121.2, "Configuring Read-Only Mappings"Opens a new window)

Supported Supported Supported

Private or Independent relationships (see Section 121.7, "Configuring Private or Independent Relationships"Opens a new window)

Supported Supported Supported

Batch reading (see Section 28.5, "Configuring Batch Reading"Opens a new window)

Supported Supported Supported

Indirection (lazy loading) (see Section 121.3, "Configuring Indirection (Lazy Loading)"Opens a new window)

Supported Supported Supported

Bidirectional relationship (see Section 121.18, "Configuring Bidirectional Relationship"Opens a new window)

Supported Supported Supported

Container policy (see Section 121.14, "Configuring Container Policy"Opens a new window)

Supported Supported Supported

Mapping comments (see Section 121.8, "Configuring Mapping Comments"Opens a new window)

Supported Supported Supported

Relational table (see Section 34.2, "Configuring a Relation Table"Opens a new window)

Supported Supported Supported

Table and field references (see Section 28.7, "Configuring Table and Field References (Foreign and Target Foreign Keys)"Opens a new window) (Source)

Supported Supported Supported

Table and field references (see Section 28.7, "Configuring Table and Field References (Foreign and Target Foreign Keys)"Opens a new window) (Target)

Supported Supported Supported

Query key order (see Section 28.6, "Configuring Query Key Order"Opens a new window)

Supported Supported Supported

Example 34-1Opens a new window shows how to create a many-to-many mapping and add it to a descriptor using Java code.

Example 34-1 Many-to-Many Mapping

public void customize(ClassDescriptor descriptor) { 
    ManyToManyMapping mapping = new ManyToManyMapping();  

    // configure mapping
    ...   

    // add mapping to descriptor
    descriptor.addMapping(mapping);
}

For more information, see the following:

For information on using JPA to configure many-to-many mappings, see "@ManyToMany" section of EclipseLink Developer's Guide at http://wiki.eclipse.org/Introduction_to_EclipseLink_JPA_%28ELUG%29#.40ManyToManyOpens a new window.

34.2 Configuring a Relation Table

The relation table contains the columns for the primary keys of the source table and target table involved in the many-to-many mapping. You must create this table in the database before completing the mapping. See Section 5.5, "Using Databases"Opens a new window for information on creating database tables.

In Figure 27-5Opens a new window, the PROJ_EMP table serves as the relation table between the PROJECT and EMPLOYEE tables.

34.2.1 How to Configure a Relation Table Using TopLink Workbench

To select a relation table for a mapping, use this procedure:

  1. Select the mapped attribute in the Navigator. Its properties appear in the Editor.

  2. Click the General tab. The General tab appears.

    Figure 34-1 Table Reference Tab, Relation Table Option

    Description of Figure 34-1 follows
    Description of "Figure 34-1 Table Reference Tab, Relation Table Option"Opens a new window

Use the Relation Table field to select a database table to define this mapping.

34.2.2 How to Configure a Relation Table Using Java

Many-to-many mappings represent the relationships between a collection of source objects and a collection of target objects. This requires an intermediate table that manages the associations between the source and target records.

Many-to-many mappings are instances of the ManyToManyMapping class and requires the following elements:

  • The attribute mapped, set by using the setAttributeName method.

  • The reference class, set by using the setReferenceClass method.

  • The relation table, set by using the setRelationTableName() method.

  • The foreign key information (for noncomposite target primary keys), which you specify by calling the setSourceRelationKeyFieldName and setTargetRelationKeyFieldName methods.

  • The foreign key information if the source or target primary keys are composite, which you specify by sending the addSourceRelationKeyFieldName or addTargetRelationKeyFieldName methods.

Example 34-2 Configuring a Relational Table

public void customize(ClassDescriptor descriptor) { 
    // In the Employee class, create the mapping that references Project class
    ManyToManyMapping manyToManyMapping = new ManyToManyMapping();
    manyToManyMapping.setAttributeName("projects");
    manyToManyMapping.setReferenceClass(Project.class);

    // Configure the relational table
    manyToManyMapping.setRelationTableName("PROJ_EMP");
    manyToManyMapping.setSourceRelationKeyFieldName ("EMPID");
    manyToManyMapping.setTargetRelationKeyFieldName ("PROJID");

    // Add mapping to descriptor
    descriptor.addMapping(manyToManyMapping);
}

In addition to the API that Example 34-2Opens a new window illustrates, other common API for use with many-to-many mappings include the following:

  • useBasicIndirection: implements TopLink value holder indirection.

  • useTransparentCollection: if you use transparent indirection, this element places a special collection in the source object's attribute.

  • dontUseIndirection: implements no indirection.

For more information about the available methods for ManyToManyMapping, see the Oracle Fusion Middleware Java API Reference for Oracle TopLinkOpens a new window.