Like ActiveRecord, ActiveJDBC has lifecycle callbacks. These are methods that can be implemented on a Model subclass to get notified of a special life cycle event performed on a model. These callbacks are captured in an interface that is implemented by a Model class:

Callback interface

See: CallbackListener

There are total of eight calls that a subclass can override to get notified of a specific event.

Registration of external listeners

You can implement the CallbackListener interface external to any model and then register it:

This is assuming that Person is a model. You can implement either the CallbackListener interface or extend CallbackAdapter (where all methods are implemented with blank bodies) and only override the ones you need.

Override Model callback methods

The Model class already extends a class CallbackAdapter, which provides empty implementations of these eight methods. All a developer needs to do is to override one or more methods to perform a task at a certain time.

Usage

Let's say we have a model User:

A user also has a password that needs to be stored in a DB in an encrypted form. Using callbacks is useful in this case, since all you have to do is to override a beforeSave() method and provide some encryption routine to make the password secure:

The framework will call beforeSave() within a context of save() or saveIt() when appropriate, and your code will encrypt the password for storage.


How to comment

The comment section below is to discuss documentation on this page.

If you have an issue, or discover bug, please follow instructions on the Support page