Managing Fields During Lifecycle Events

While a persistent instance is in memory, it transitions through certain lifecycle events, as we described in Chapter 11. You may want to execute some functionality when these events occur. For example, if you have a persistent class with nonpersistent fields, you may want to initialize the values of the fields when instances from the datastore are instantiated in memory. This is enabled in JDO by a mechanism called an instance callback .

JDO defines the InstanceCallbacks interface to support instance callbacks. This interface has four methods, each of which is called when a particular lifecycle event occurs. If you declare that a persistent class implements the InstanceCallbacks interface, the following methods must be defined and are called when their associated lifecyle event occurs:

void jdoPostLoad( )

Called for an instance after the values have been loaded into its default fetch group fields. This occurs when the instances transition from hollow to persistent-clean or persistent-dirty. In this method, you should initialize nonpersistent fields that depend on fields in the default fetch group. Another use for this method is to register it with other objects in the runtime environment.

The enhancer does not add field mediation code to this method; so, you should access only fields in the default fetch group, since you are not guaranteed that the other fields have been fetched. The context in which jdoPostLoad( ) is called does not allow ...

Get Java Data Objects now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.