Skip to content

Lifecycle

Don Peter edited this page Oct 19, 2017 · 1 revision

Lifecycle Events

ONAM has support for four types of lifecycle functions, that you can make use of to do validations, pre or post processing.

ONAM provides @BeforeCreate, @AfterCreate, @BeforeUpdate, @AfterUpdate annotations.

You may define any number of functions and tag them with the above annotations in your extended Entity classes.

Lets say you want to set created_at and updated_at fields just before insertion to database.

    @BeforeCreate
    public void settingTimeStamps(){
        this.created_at = System.currentTimeMillis();
        this.update_at = System.currentTimeMillis();
    }

Or separately,

    @BeforeCreate
    public void setCreatedAt(){
        this.created_at = System.currentTimeMillis();
    }
    @BeforeCreate
    public void setUpdatedAt(){
        this.update_at = System.currentTimeMillis();
    }

Both functions will be executed before insertion.
@AfterCreate will be executed after insertion to the database.

@BeforeUpdate will be executed before update to the database.

    @BeforeUpdate
    public void setUpdatedAt(){
        this.update_at = System.currentTimeMillis();
    }

@AfterUpdate will be executed after update to the database.

The changes done by @BeforeCreate and @BeforeUpdate functions are persistent.

If multiple functions are annotated with same annotation in a single extended Entity class file, then their execution will not be at any particular order.

Clone this wiki locally