Oracle® Application Server TopLink Mapping Workbench User's Guide
10g Release 2 (10.1.2) Part No. B15900-01 |
|
Previous |
Next |
Indirection allows you to retrieve objects from the database as needed.
With indirection turned off, when an object is retrieved from the database all the other objects that it references are also retrieved.
With indirection turned on, each object is retrieved from the database only when asked for.
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.
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.
After modifying the code, update the OracleAS TopLink Mapping Workbench descriptors to use indirection.
To implement indirection in the Workbench:
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.