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 One-to-One Mappings

One-to-one mappings represent simple pointer references between two Java objects. In Java, a single pointer stored in an attribute represents the mapping between the source and target objects. Relational database tables implement these mappings using foreign keys.

Figure 6-11 illustrates a one-to-one relationship from the address attribute of an Employee object to an Address object. To store this relationship in the database, create a one-to-one mapping between the address attribute and the Address class. This mapping stores the id of the Address instance in the EMPLOYEE table when the Employee instance is written. It also links the Employee instance to the Address instance when the Employee is read from the database. Because an Address does not have any references to the Employee, it does not have to provide a mapping to Employee.

For one-to-one mappings, the source table normally contains a foreign key reference to a record in the target table. In Figure 6-11, the ADDR_ID field of the EMPLOYEE table is a foreign key.

Figure 6-11 One-to-One Mappings

Description of 11mapfig.gif follows
Description of the illustration 11mapfig.gif

You can also implement a one-to-one mapping where the target table contains a foreign key reference to the source table. In the example, the database design would change such that the ADDRESS row would contain the EMP_ID to identify the Employee to which it belonged. In this case, the target must also have a relationship mapping to the source.The update, insert and delete operations, which are normally done for the target before the source, for privately owned one-to-one relationships, are performed in the opposite order when the target owns the foreign key. Target foreign keys normally occur in bidirectional one-to-one mappings, because one side has a foreign key and the other shares the same foreign key in the other's table.

Target foreign keys can also occur when large cascaded composite primary keys exist (that is, one object's primary key is composed of the primary key of many other objects). In this case it is possible to have a one-to-one mapping that contains both foreign keys and target foreign keys.

In a foreign key, OracleAS TopLink automatically updates the foreign key value in the object's row. In a target foreign key, it does not. In OracleAS TopLink, the Target Foreign Key checkbox includes a checkmark when a target foreign key relationship is defined.

When mapping a relationship, you must understand these differences between a foreign key and a target foreign key, to ensure that the relationship is defined correctly.

In a bidirectional relationship where the two classes in the relationship reference each other, only one of the mappings should have a foreign key. The other mapping should have a target foreign key. If one of the mappings in a bidirectional relationship is a one-to-many mapping, see "Working with Variable One-to-One Mappings" for details.

Creating One-to-One Mappings

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

To create a one-to-one mapping:

  1. One-to-One Mapping button.
    Description of the illustration 11mapbtn.gif

    In the Navigator pane, select the mapping to be mapped and click the One-to-One Mapping button on the mapping toolbar.

    The One-to-one mapping tab appears in the Editor pane.

Figure 6-12 One-to-One Mapping General Properties

Description of 11maptab.gif follows
Description of the illustration 11maptab.gif

  1. Enter the required information on the General tab (see "Working with Common Mapping Properties" on page 4-70).

  2. You can also specify:

    • Bidirectional relationships – See "Maintaining Bidirectional Relationships" on page 4-73

    • Read-only attributes – See "Specifying Read-Only Settings" on page 4-72

    • Access methods – See "Specifying Direct Access and Method Access" on page 4-71

    • Null values – See "Defaulting Null Values" on page 4-73

  3. Click the Table Reference tab to choose the reference.

Figure 6-13 One-to-One Mapping Table Reference Properties

Description of 11mapref.gif follows
Description of the illustration 11mapref.gif

  1. Use this table to enter data in each field:

Field Description
Table Reference Use the drop-down list to choose a table reference for the mapping. Click New to create a new table
Key Pairs
Source Field Use the drop-down list to choose a field from the source table.
Target Field Use the drop-down list to choose a field from the target table.
Target Foreign Key Specify if the relationship is a target foreign key.

Specifying Advanced Features Available by Amending the Descriptor

One-to-one target objects mapped as Privately Owned are, by default, verified before deletion or update outside of a unit of work.

Verification is a check for the previous value of the target and is accomplished through joining the source and target tables. Inside a unit of work, verification is accomplished by obtaining the previous value from the back-up clone, so this setting is not used because a database read is not required. You may wish to disable verification outside of a unit of work for performance reasons and can do so by sending the setShouldVerifyDelete() message to the mapping in an amendment method written for the descriptor, as follows:

public static void addToDescriptor(Descriptor descriptor){
//Find the one-to-one mapping for the address attribute
OneToOneMapping addressMapping=(OneToOneMapping) descriptor.getMappingForAttributeName(ÒaddressÓ);
addressMapping.setShouldVerifyDelete(false);
}