1咆槽、beforeInsert - 在對象初始持久保存到數(shù)據(jù)庫之前執(zhí)行来吩。如果返回false俐银,插入將被取消岔绸。
afterInsert - 在對象持久化到數(shù)據(jù)庫之后執(zhí)行
class Person {
private static final Date NULL_DATE = new Date(0)
String firstName
String lastName
Date signupDate = NULL_DATE
def beforeInsert() {
if (signupDate == NULL_DATE) {
signupDate = new Date()
}
}
}
=================================================================================================
2理逊、beforeUpdate - 在更新對象之前執(zhí)行。如果返回false盒揉,更新將被取消晋被。
afterUpdate - 在對象更新后執(zhí)行
class Person {
def securityService
String firstName
String lastName
String lastUpdatedBy
static constraints = {
lastUpdatedBy nullable: true
}
def beforeUpdate() {
lastUpdatedBy = securityService.currentAuthenticatedUsername()
}
}
=================================================================================================
3、beforeDelete - 在刪除對象之前執(zhí)行刚盈。如果返回false羡洛,刪除將被取消。
afterDelete - 在對象被刪除后執(zhí)行
class Person {
String name
def beforeDelete() {
ActivityTrace.withNewSession {
new ActivityTrace(eventName: "Person Deleted", data: name).save()
}
}
}
=================================================================================================
4扁掸、beforeValidate - 在對象驗證之前執(zhí)行
class Person {
String name
static constraints = {
name size: 5..45
}
def beforeValidate() {
name = name?.trim()
}
}
=================================================================================================
5最域、onLoad - 從數(shù)據(jù)庫加載對象時執(zhí)行
class Person {
String name
Date dateCreated
Date lastUpdated
def onLoad() {
log.debug "Loading ${id}"
}
}