I am new to Grails and I am curious about domain class hooks like beforeinsert()
, beforeupdate()
, ... etc.
But I can't find a good resource/documentation on how to use this feature.
Can someone give me a good resource or explain to me how this things work?
These methods are a way of hooking into the domain instance lifecylce.
The grails gorm documentation is very clean and precise so I will quote the definitions from there but I created a state chart diagram to depict the picture:
- beforeInsert - Executed before an object is initially persisted to the database. If you return false, the insert will be cancelled.
- beforeUpdate - Executed before an object is updated. If you return false, the update will be cancelled.
- beforeDelete - Executed before an object is deleted. If you return false, the delete will be cancelled.
- beforeValidate - Executed before an object is validated
- afterInsert - Executed after an object is persisted to the database
- afterUpdate - Executed after an object has been updated
- afterDelete - Executed after an object has been deleted
- onLoad - Executed when an object is loaded from the database