Skip Headers

Oracle® Application Development Framework Developer's Guide 10g Release 3 (10.1.3) B25386-01 |
| Home | Solution Area | Contents | Index

Previous | Next |

# 3.5 Mapping Related Classes with Relationships

Relational mappings define how persistent objects reference other persistent objects. TopLink provides the following relationship mappings:

- Direct collection mappings - Map Java collections of objects that do not have descriptors.

- Aggregate object mappings - Strict one-to-one mappings that require both objects to exist in the same database row.

- One-to-one mappings - map a reference to another persistent Java object to the database.

- Variable one-to-one mappings - Map a reference to an interface to the database.

- One-to-many mappings - Map Java collections of persistent objects to the database.

- Aggregate collection mappings also map Java collections of persistent objects to the database.

- Many-to-many mappings use an association table to map Java collections of persistent objects to the database

Do not confuse relational mappings with object-relational mappings. Object-relational mappings let you map an object model into an object-relational data model, such as Oracle Database. TopLink can create the following mappings:

- Object-Relational Structure Mapping – Map to object-relational aggregate structures (the `Struct` type in JDBC and the `OBJECT` type in Oracle Database)

- Object-Relational Reference Mapping – Map to object-relational references (the `Ref` type in JDBC and the `REF` type in Oracle Database)

- Object-Relational Array Mapping – Map a collection of primitive data to object-relational array data types (the Array type in JDBC and the VARRAY type in Oracle Database).

- Object-Relational Object Array Mapping – Map to object-relational array data types (the Array type in JDBC and the VARRAY type in Oracle Database).

- Object-Relational Nested Table Mapping – Map to object-relational nested tables (the Array type in JDBC and the NESTED TABLE type in Oracle Database)

Although the Oracle TopLink runtime supports these mappings, they must be created in Java code – you cannot use the Mapping editor.

## 3.5.1 How to Create Relationship Mappings

Similarly to direct mappings (see Section 3.4.3, "How to Create Direct Mappings"), to map create Java classes directly to database tables, select the Java attribute in the TopLink Map – Structure window.

Relationship mappings contain a Table Reference tab in the Mapping editor to define (or create) relationships on the database tables.

Figure 3-11 Sample Table Reference Tab

Refer to the Oracle JDeveloper online help for more information.

## 3.5.2 What Happens when you Create a Relationship

Example 3-5 illustrates the Java code that Oracle JDeveloper generates when you create a direct-to-field direct mapping. In this example, the `address` attribute of the `ServiceRequest` class has a one-to-one relationship to another class, `User` (that is, each `ServiceRequest` was created by one `User`)

Example 3-5 Java Code for a Relationship Mapping

    package oracle.srdemo.model;

        /**Map createdBy <-> oracle.srdemo.model.Users
         * @associates <{oracle.srdemo.model.Users}>
         */
      private ValueHolderInterface createdBy;

        public Users getCreatedBy() {
          return (Users)this.createdBy.getValue();
        }

        public void setCreatedBy(Users createdBy) {
          this.createdBy.setValue(createdBy);
        }

## 3.5.3 What You May Need to Know

Use the Mapping editor to customize the TopLink mappings. Some common customizations for relationship mappings include:

- Specifying the mapping as "read only." These mappings will not be included during update or delete operations.

- Using custom `get` and `set` methods.

- Defining a default value. This value will be used if the actual field in the database is null.

- Using indirection. When using indirection, TopLink uses an indirection object as a placeholder for the referenced object: TopLink defers reading the dependent object until you access that specific attribute.

- Configuring private or independent relationships. In a private relationship, the target object is a private component of the source object; destroying the source object will also destroy the target object. In an independent relationship, the source and target objects exist independently; destroying one object does not necessarily imply the destruction of the other.

- Specifying bidirectional relationship in which the two classes in the relationship reference each other with one-to-one mappings

Figure 3-12 shows the General tab of a one-to-one mapping in the Mapping editor. Use the Table Reference tab (see Figure 3-13) to define the foreign key reference for the mapping. Each direct mapping (see Section 3.5, "Mapping Related Classes with Relationships") may have additional, specific options as well. Refer to the Oracle JDeveloper online help for more information.

Figure 3-12 Sample One-to-One Mapping, General Tab

Description of "Figure 3-12 Sample One-to-One Mapping, General Tab"

Figure 3-13 Sample One-to-One Mapping, Table Reference Tab

Description of "Figure 3-13 Sample One-to-One Mapping, Table Reference Tab"