Oracle TopLink Developer's Guide 10g Release 3 (10.1.3) B13593-01 |
|
![]() 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 37-9 summarizes which relational mappings support this option.
Table 37-9 Relational Mapping Support for Table Reference
Mapping | Using TopLink Workbench |
Using Java |
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.
To specify a table for a mapping reference, use this procedure:
Select the mapped attribute in the Navigator. Its properties appear in the Editor.
Click the Table Reference tab. The Reference tab appears.
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. |