Lifecycle callbacks
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
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:
1 2 3 4 5 6 7 8 |
|
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
:
1 |
|
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:
1 2 3 4 5 6 7 8 |
|
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