Skip Headers

# Configuring ValueHolder Indirection With Method Accessing

Previous

Next

If you are using `ValueHolder` indirection with method accessing (see "Configuring Method Accessing"), in addition to changing your attributes types in your Java code to `ValueHolderInterface`, you must also provide TopLink with two pairs of getter and setter methods:

- getter and setter of the indirection object that are registered with the mapping and used only by TopLink. They include a `get` method that returns an instance that conforms to `ValueHolderInterface`, and a `set` method that accepts one argument that conforms to the same interface.

- getter and setter of the actual attribute value used by the application

Example: Class Using ValueHolder Indirection illustrates the `Employee` class using `ValueHolder` indirection with method access. The class definition is modified so that the `address` attribute of `Employee` is a `ValueHolderInterface` instead of an `Address`, and appropriate getter and setter methods are supplied.

Class Using ValueHolder Indirection with Method Accessing

    public class Employee {

        protected ValueHolderInterface address;

        // Initialize ValueHolders in constructor
        public Employee() {
            address = new ValueHolder();
        }

        // getter and setter registered with the mapping and used only by TopLink
        public ValueHolderInterface getAddressHolder() {
            return address;
        }
        public void setAddressHolder(ValueHolderInterface holder) {
            address = holder;
        }

        // getter and setter methods used by the application to access the attribute
        public Address getAddress() {
            return (Address) address.getValue();
        }
        public void setAddress(Address theAddress) {
            address.setValue(theAddress);
        }
    }

Example: Mapping Using ValueHolder Indirection shows how to configure a one-to-one mapping to the `address` attribute.

Mapping Using ValueHolder Indirection with Method Accessing

    OneToOneMapping mapping = new OneToOneMapping();
    mapping.useBasicIndirection();
    mapping.setReferenceClass(Employee.class);
    mapping.setAttributeName("address");
    mapping.setGetMethodName("getAddressHolder");
    mapping.setSetMethodName("setAddressHolder");

The application uses `Employee` methods `getAddress` and `setAddress` to access the `Address` object. Because basic indirection is enabled, TopLink uses `Employee` methods `getAddressHolder` and `setAddressHolder` methods when performing persistence operations on instances of `Employee`.