Oracle® Application Server TopLink Mapping Workbench User's Guide
10g Release 2 (10.1.2) Part No. B15900-01 |
|
Previous |
Next |
One-to-many mappings are used to represent the relationship between a single source object and a collection of target objects. They are a good example of something that is simple to implement in Java using a Vector
(or other collection types) of target objects, but difficult to implement using relational databases.
In a Java Vector
, the owner references its parts. In a relational database, the parts reference their owner. Relational databases use this implementation to make querying more efficient.
Note: See "Working with a Container Policy" for information on using collection classes other thanVector with one-to-many mappings.
|
The purpose of creating this one-to-one mapping in the target is so that the foreign key information can be written when the target object is saved. Alternatives to the one-to-one mapping back reference include:
Use a direct-to-field mapping to map the foreign key and maintain its value in the application. Here the object model does not require a back reference, but the data model still requires a foreign key in the target table.
Use a many-to-many mapping to implement a logical one-to-many. This has the advantage of not requiring a back reference in the object model and not requiring a foreign key in the data model. In this model the many-to-many relation table stores the collection. It is possible to put a constraint on the join table to enforce that the relation is a logical one-to-many relationship.
Figure 6-23 One-to-Many Mapping One-to-many mappings must put the foreign key in the target table, rather than the source table. The target class should also implement a one-to-one mapping back to the source object, as illustrated in the following figure.One-to-Many Relationships
Use this procedure to create a one-to-many mapping in the OracleAS TopLink Mapping Workbench.
To create a one-to-many mapping:
Select the attribute to be mapped from the Navigator pane.
Description of the illustration 1mmapbtn.gif
Click the One-to-Many Mapping button on the mapping toolbar.
Use the Reference Descriptor drop-down list to choose the reference for this descriptor.
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.
Click the Table References tab to specify foreign key information for this mapping. See "Creating Table References" on page 3-6 for more information.