Oracle TopLink Developer's Guide
10g Release 3 (10.1.3) B13593-01 |
|
![]() Previous |
![]() Next |
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 36-2 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 36-2, the ADDR_ID
field of the EMPLOYEE table is a foreign key.
You can also implement a one-to-one mapping where the target table contains a foreign key reference to the source table. In Figure 36-2, 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, TopLink automatically updates the foreign key value in the object's row. In a target foreign key, it does not. In TopLink, use the Target Foreign Key option 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 "Configuring a Relational Variable One-to-One Mapping" for details.
You can use a one-to-one mapping with a change policy (see "Configuring Change Policy".
See "Configuring a Relational One-to-One Mapping" for more information.
To maintain EJB compliance, the object attribute that points to the target of the relationship must be the remote (EJB 1.1) or local (EJB 2.0) interface type–not the bean class.
TopLink provides variations on one-to-one mappings that lets you define complex relationships when the target of the relationship is a dependent Java object. For example, variable one-to-one mappings enable you to specify variable target objects in the relationship. These variations are not available for entity beans, but are valid for dependent Java objects.
For more information, see the Chapter 41, "Configuring a Relational Variable One-to-One Mapping".