Skip Headers

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

Previous

Next

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: Relational Mapping Support for Table Reference summarizes which relational mappings support this option.

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.