Skip Headers

Oracle TopLink Developer's Guide 10g Release 3 (10.1.3) B13593-01 |
| Home | Solution Area | Contents | Index

Previous | Next |

# Configuring Table and Field References (Foreign and Target Foreign Keys)

A foreign key is a combination of one or more database columns that reference a unique key, usually the primary key, in another table. Foreign keys can be any number of fields (similar to a primary key), all of which are treated as a unit. A foreign key and the parent key it references must have the same number and type of fields.

Mappings that extend `oracle.toplink.mappings.ForeignReferenceMapping` use foreign keys to find information in the database so that the target object(s) can be instantiated. For example, if every `Employee` has an attribute `address` that contains an instance of `Address` (which has its own descriptor and table) then, the one-to-one mapping for the `address` attribute would specify foreign key information to find an `Address` for a particular `Employee`.

TopLink classifies foreign keys into two categories in mappings–foreign keys and target foreign keys:

- In a foreign key, the key is found in the table associated with the mapping's own descriptor. For example, an `Employee` foreign key to `ADDRESS` would be in the `EMPLOYEE` table.

- In a target foreign key, the reference is from the target object's table back to the key from the mapping's descriptor's table. For example, the `ADDRESS` table would have a foreign key to `EMPLOYEE`.

  Caution: Make sure you fully understand the distinction between foreign key and target foreign key before defining a mapping.

The table reference is the database table that contains the foreign key references.

Table 37-9 summarizes which relational mappings support this option.

Table 37-9 Relational Mapping Support for Table Reference

Mapping | Using TopLink Workbench | Using Java
--- | --- | ---
One-to-One Mapping |  |
One-to-Many Mapping |  |
Many-to-Many Mapping |  |
Aggregate Collection Mapping |  |
Direct Collection Mapping |  |
Direct Map Mapping |  |

Using TopLink Workbench, you can either import this table from your database or create it. If you import tables from the database (see "Importing Tables from a Database"), TopLink creates references that correspond to existing database constraints (if supported by the driver). You can also define references in TopLink without creating similar constraints on the database.

## Using TopLink Workbench

To specify a table for a mapping reference, use this procedure:

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

2. Click the Table Reference tab. The Reference tab appears.

   Figure 37-5 Table Reference Tab, Table Reference Field

   Description of "Figure 37-5 Table Reference Tab, Table Reference Field"

Use the following information to select the field references on the tab:

Field | Description
--- | ---
Table Reference | Select an existing table, or click New to create a new table reference.
Source and Target Field | Click Add to create new foreign key reference. To delete an existing key pair reference, select the Source and Target fields and click Remove.
Source Field | Select the database field from the source table for this foreign key reference.
Target Field | Select the database field from the target table for this foreign key reference.
Target Foreign Key | Specify whether or not the reference is from the target object's table back to the key from the mapping's descriptor's table.