Skip Headers
Oracle® Application Development Framework Developer's Guide
10g Release 3 (10.1.3)
B25386-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
 

3.5 Mapping Related Classes with Relationships

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

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:

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

image of the 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

welcome screen from Creating Java Objects from Tables wizard
Description of "Figure 3-12 Sample One-to-One Mapping, General Tab"

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

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