Skip Headers

# Configuring Proxy Indirection

Previous

Next

Example: Proxy indirection Examples illustrates an `Employee` to `Address` one-to-one relationship.

Proxy indirection Examples

    public interface Employee {
        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: Proxy indirection Examples, both the `EmployeeImpl` and the `AddressImpl` classes implement public interfaces (`Employee` and `Address` respectively). Therefore, because the `AddressImpl` class 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 in the following example:

    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 an insert operation. You still use the following code:

    Employee emp = new EmployeeImpl("John Smith");
    Address add = new AddressImpl("123 Main St.");
    emp.setAddress(add);