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 immutable 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. For example, suppose you need to persist a `java.net.InetAddress`, which is not directly supported by JDO. You could use a persistent `String` field to hold the hostname of the address, and then in `jdoPostLoad` you could use this string to create the actual `InetAddress` instance and cache it in a non-persistent field of your object.

- `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. Returning to our `InetAddress` example, `jdoPreStore` would be used to retrieve the host name from the non-persistent `InetAddress` field and store it in the persistent `String` field.

- 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.

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