Oracle® Application Server TopLink Mapping Workbench User's Guide
10g Release 2 (10.1.2) Part No. B15900-01 |
|
Previous |
Next |
Introduced in JDK 1.3, the Java class Proxy
enables you to use dynamic proxy objects as stand-ins for a defined interface. Certain OracleAS TopLink mappings (OneToOneMapping
, VariableOneToOneMapping
, ReferenceMapping
, and TransformationMapping
) can be configured to use proxy indirection, which gives you the benefits of OracleAS TopLink indirection without the need to include OracleAS TopLink classes in your domain model. Proxy indirection is to one-to-one relationship mappings as indirect containers are to collection mappings.
Although the OracleAS TopLink Mapping Workbench does not support proxy indirection, you can use the useProxyIndirection
method in an amendment method.
To use proxy indirection, your domain model must satisfy the following criteria:
The target class of the one-to-one relationship must implement a public interface.
The one-to-one attribute on the source class must be of the interface type.
If you employ method accessing, then the get()
and set()
methods must use the interface.
Example 6-2 Proxy indirection Examples
The following code illustrates an Employee->Address one-to-one relationship.
public String getName(); public Address getAddress(); public void setName(String value); public void setAddress(Address value); . . . } public class EmployeeImpl implements Employee { public String name; public Address address; . . . public Address getAddress() { return this.address; } public void setAddress(Address value) { this.address = value; } } public interface Address { public String getStreet(); public void setStreet(String value); . . . } public class AddressImpl implements Address { public String street; . . . }
In Example 6-2, both the EmployeeImpl and the AddressImpl classes implement public interfaces (Employee and Address respectively). Therefore, because the AddressImpl is the target of the one-to-one relationship, it is the only class that must implement an interface. However, if the EmployeeImpl is ever to be the target of another one-to-one relationship using transparent indirection, it must also implement an interface, as shown below:
Employee emp = (Employee) session.readObject(Employee.class);System.out.println(emp.toString()); System.out.println(emp.getAddress().toString()); // Would print: [Employee] John Smith { IndirectProxy: not instantiated } String street = emp.getAddress().getStreet(); // Triggers database read to get Address information System.out.println(emp.toString()); System.out.println(emp.getAddress().toString()); // Would print [Employee] John Smith { [Address] 123 Main St. } Using proxy indirection does not change how you instantiate your own domain objects for insert. You still use the following code: Employee emp = new EmployeeImpl("John Smith"); Address add = new AddressImpl("123 Main St."); emp.setAddress(add);
To enable proxy indirection in Java code, use the following API for ObjectReferenceMapping
:
useProxyIndirection()
– Indicates that OracleAS TopLink should use proxy indirection for this mapping. When the source object is read from the database, a proxy for the target object is created and used in place of the ÒrealÓ target object. When any method other than getString()
is called on the proxy, the ÒrealÓ data will be read from the database.
Example 6-3 Proxy indirection Example
The following code example illustrates using proxy indirection.
// Define the 1:1 mapping, and specify that Proxy Indirection should be used OneToOneMapping addressMapping = new OneToOneMapping(); addressMapping.setAttributeName("address"); addressMapping.setReferenceClass(AddressImpl.class); addressMapping.setForeignKeyFieldName("ADDRESS_ID"); addressMapping.setSetMethodName("setAddress"); addressMapping.setGetMethodName("getAddress"); addressMapping.useProxyIndirection(); descriptor.addMapping(addressMapping); . . .