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
 

Implementing Indirection

Indirection allows you to retrieve objects from the database as needed.

Using indirection can be a great performance benefit and we strongly recommended using it. See "Working with Indirection" on page 6-5 for more information.

Preparing Java Code for Indirection

To prepare your object model for indirection, you must alter the application slightly:

  • Replace each relationship reference with a ValueHolderInterface. This interface is located in the oracle.toplink.indirection package and allows for indirection to be used.

  • Instantiate all variables with indirection references to empty value holders. Normally, this is done in the constructor of the object.

  • Modify the get methods for these variables to extract the value from the value holder.

  • Modify the set methods for these variables to insert the value into the value holder.

You can implement indirection using direct access or method access.

  • For method access, TopLink requires additional get and set methods that provide access to the value holders.

  • For direct access, TopLink can access the value holders directly—the additional get and set methods are not required.

If the instance variable returns a Vector instead of an object, then define the value holder in the constructor as follows:

addresses = new ValueHolder(new Vector());

In the following examples, the Employee class uses indirection with method access for its one-to-one mapping to Address. The class definition is modified so that the address attribute of Employee is a ValueHolderInterface instead of an Address. In both examples, the application uses the getAddress() and setAddress() methods to access the Address object.

Example B-6 Indirection Examples

The following example illustrates code before using indirection.

protected Address address;
    public Employee() {
        address = null;
    }
    public Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }

The following example illustrates the same code after using indirection.

protected ValueHolderInterface address;
    public Employee() {
        address = new ValueHolder();
    }
    public Address getAddress() {
        return (Address)address.getValue();
    }
    public void setAddress(Address address) {
        this.address.setValue(address);
    }

The indirection example could also use method access instead of direct access. This would be implemented by adding getAddressValueHolder() and setAddressValueHolder() methods.

Implementing Indirection in the OracleAS TopLink Mapping Workbench

After modifying the code, update the OracleAS TopLink Mapping Workbench descriptors to use indirection.

To implement indirection in the Workbench:

  1. Map the one-to-one and one-to-many mappings for each class as normal.

  2. On the General tab for each mapping, select the Use Indirection option.

Figure B-31 General Tab of a Mapping

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

Implementing Indirection in the Tutorial

The following attributes in the Advanced tutorial sample code have been implemented using ValueHolderInterfaces:

Employee

address manager managedEmployees projects responsibilitiesList phoneNumbers

PhoneNumber

owner

BaseProject

teamLeader

When you create mappings for these attributes, be sure to enable the Use Indirection option.