Skip Headers

# Configuring IndirectContainer Indirection

Previous

Next

Instances of `oracle.toplink.mappings.ForeignReferenceMapping` and `oracle.toplink.mappings.foundation.AbstractTransformationMapping` provide the `useContainerIndirection` method to configure a mapping to an attribute that you code with an `oracle.toplink.indirection.IndirectContainer` between it and the real object.

Using an `IndirectContainer`, a `java.util.Collection` class can act as a TopLink indirection object: the `Collection` will only read its contents from the database when necessary (typically, when a `Collection` accessor is invoked). Without an `IndirectContainer`, all members of the `Collection` must be retrieved when the `Collection` attribute is accessed.

Example: Class Using ValueHolder Indirection illustrates the `Employee` class using `IndirectContainer` indirection with method access. The class definition is modified so that the `addresses` attribute of `Employee` is an `IndirectContainer` instead of an `Address`es, and appropriate getter and setter methods are supplied. In this example, `addresses` is a `java.util.List`, so an instance of `oracle.toplink.indirection.IndirectList` is used.

Class Using IndirectContainer Indirection

    public class Employee {

        protected IndirectList addresses;

        // Initialize ValueHolders in constructor
        public Employee() {
            addresses = new IndirectList();
        }

        // getter and setter methods registered with the mapping and used by TopLink
        public ValueHolderInterface getAddressesHolder() {
            return addresses.getValueHolder();
        }
        public void setAddressesHolder(ValueHolderInterface holder) {
            addresses = addresses.setValueHolder(holder);
        }

        // getter and setter methods used by the application to access the attribute
        public List getAddresses() {
            return (List) addresses.getValue();
        }
        public void setAddresses(List newAddresses) {
            addresses.removeAll();
            addresses.addAll(newAddresses);
        }
    }

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

Mapping Using IndirectContainer Indirection

    OneToOneMapping mapping = new OneToOneMapping();
    mapping.useBasicIndirection();
    mapping.setReferenceClass(Employee.class);
    mapping.setAttributeName("addresses");
    mapping.setGetMethodName("getAddressesHolder");
    mapping.setSetMethodName("setAddressesHolder");