4.4. InstanceCallbacks
Prev | Chapter 4. PersistenceCapable | Next

## 4.4. InstanceCallbacks

Your persistent classes can implement the `javax.jdo.InstanceCallbacks` interface to receive callbacks when certain JDO lifecycle events take place. This interface consists of four methods:

- The `jdoPostLoad` method is called by the JDO implementation after the default fetch group fields of your class have been loaded from the data store. Default fetch groups are explained in the section on JDO metadata; for now think of the default fetch group as all of the primitive fields of the object. No other persistent fields can be accessed in this method.

  `jdoPostLoad` is often used to initialize non-persistent fields whose values depend on the values of persistent fields. An example of this is presented below.

- `jdoPreStore` is called just before the persistent values in your object are flushed to the data store. You can access all persistent fields in this method.

  `jdoPreStore` is the complement to `jdoPostLoad`. While `jdoPostLoad` is most often used to initialize non-persistent values from persistent data, `jdoPreStore` is usually used to set persistent fields with information cached in non-persistent ones. See the example below.

- The `jdoPreClear` method is called before the persistent fields of your object are cleared. JDO implementations clear the persistent state of objects for several reasons, most of which will be covered later in this document. `jdoPreClear` can be used to clear non-persistent cached data and null relations to other objects. You should not access the values of persistent fields in this method.

- `jdoPreDelete` is called before an object is deleted from the data store. Access to persistent fields is valid within this method. You might implement privately-owned relations by using this method to delete other related objects.

Unlike the `PersistenceCapable` interface, you must implement the `InstanceCallbacks` interface explicitly if you want to receive lifecycle callbacks.

Example 4.3. Using the InstanceCallbacks Interface

    /**
     * Example demonstrating the use of the InstanceCallbacks interface to
     * persist a java.net.InetAddress and implement a privately-owned relation.
     */
    public class Host
        implements InstanceCallbacks
    {
        // the InetAddress field cannot be persisted directly by JDO, so we
        // use the jdoPostLoad and jdoPreStore methods below to persist it
        // indirectly through its host name string
        private transient InetAddress address;    // non-persistent
        private String                hostName;   // persistent

        // set of devices attached to this host
        private Set devices = new HashSet ();

        // setters, getters, and business logic omitted

        public void jdoPostLoad ()
        {
            // form the InetAddress using the persistent host name
            try
            {
                address = InetAddress.getByName (hostName);
            }
            catch (IOException ioe)
            {
                throw new JDOException ("Invalid host name: " + hostName, ioe);
            }
        }

        public void jdoPreStore ()
        {
            // store the host name information based on the InetAddress values
            hostName = address.getHostName ();
        }

        public void jdoPreDelete ()
        {
            // delete all related devices when this host is deleted
            JDOHelper.getPersistenceManager (this).deletePersistentAll (devices);
        }

        public void jdoPreClear ()
        {
        }
    }

Prev | Up | Next
4.3. Restrictions on Persistent Classes | Home | 4.5. JDO Identity