Oracle® Application Server TopLink Mapping Workbench User's Guide
10g Release 2 (10.1.2) Part No. B15900-01 |
|
Previous |
Next |
Using indirection objects can improve the performance of OracleAS TopLink object relationships. An indirection object takes the place of an application object so that the application object is not read from the database until it is needed (see Figure 6–1).
Without indirection, when OracleAS TopLink retrieves a persistent object, it also retrieves all the objects referenced by that object. This can result in lower performance for some applications. Using indirection allows OracleAS TopLink to create Òstand-insÓ for related objects, resulting in significant performance improvements, especially when the application is interested only in the contents of the retrieved object rather than the objects to which it is related.
Indirection is available for transformation mappings and for direct collection, one-to-one, one-to-many, and many-to-many relationship mappings.
You can enable or disable indirection for each mapping individually. By default, indirection is enabled for relationship mappings and disabled for transformation mappings. Indirection should be enabled only for transformation mappings if the execution of the transformation method is a resource-intensive task, such as accessing the database.
Indirection disabled: An indirection object is not used. Whenever an object is retrieved from the database, all the objects associated with it through the mapping are also read.
Indirection enabled: A value holder is used to represent the entire relationship. When an object is retrieved from the database, a value holder is created and stored in the attribute corresponding to the mapping. The first time the value holder is accessed, it retrieves the related object from the database.
In addition to this standard version of indirection, collection mappings (direct collection, one-to-many, and many-to-many) can use indirect containers.
Persistent classes that use indirection must replace relationship attributes with value holder attributes. A value holder is an instance of a class that implements the ValueHolderInterface
interface, such as ValueHolder
. This object stores the information necessary to retrieve the object it is replacing from the database. If the application does not access the value holder, the replaced object is never read from the database.
When using method access, the get
and set
methods specified for the mapping must access an instance of ValueHolderInterface
, rather than the object referenced by the value holder.
To obtain the object that the value holder replaces, use the getValue()
and setValue()
methods of the ValueHolderInterface
class. A convenient way of using these methods is to hide the getValue
and setValue
methods of the ValueHolderInterface
inside get
and set
methods, as in the following example.
The following figure illustrates the Employee object being read from the database. The Address object is not read and will not be created unless it is accessed.
The first time the address is accessed, as in the following figure, the ValueHolder reads and returns the Address object.
Subsequent requests for the address do not access the database, as shown in the following figure.
Use this procedure to specify that a mapping uses indirection.
To specify indirection:
In the Navigator pane, select the mapping to be mapped, and click the appropriate button on the mapping toolbar.
The mapping tab appears in the Editor pane.
On the General tab, click Use Indirection to specify that the mapping uses indirection.
Attributes using indirection must conform to the ValueHolderInterface
. You can change your attribute types in the Class Editor without re-importing your Java classes. Ensure that you change the attribute types in your Java code as well. Attributes typed incorrectly will be marked as deficient.
In addition to changing the attribute's type, you may also need to change its accessor methods. If you use method access, OracleAS TopLink requires accessors to the indirection object itself, so your get method returns an instance that conforms to ValueHolderInterface
, and your set method accepts one argument that conforms to the same. If the instance variable returns a Vector instead of an object, then the value holder should be defined in the constructor as follows:
addresses = new ValueHolder(new Vector());
In any case, the application uses the getAddress()
and setAddress()
methods to access the Address
object. With indirection, OracleAS TopLink uses the getAddressHolder()
and setAddressHolder()
methods when saving and retrieving instances to and from the database.
Refer to the Oracle Application Server TopLink Application Developer's Guide for details.
Example 6-1 IndirectionThe following code illustrates the Employee class using indirection with method access for a 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, and appropriate get and set methods are supplied./
/ Initialize ValueHolders in Employee Constructor public Employee() { address = new ValueHolder(); } protected ValueHolderInterface address; // 'Get' and 'Set' accessor methods registered with the mapping and used by OracleAS TopLink. public ValueHolderInterface getAddressHolder() { return address; } public void setAddressHolder(ValueHolderInterface holder) { address = holder; } // 'Get' and 'Set' accessor methods used by the application to access the attribute. public Address getAddress() { return (Address) address.getValue(); } public void setAddress(Address theAddress) { address.setValue(theAddress); }