1、beforeInsert - 在对象初始持久保存到数据库之前执行。如果返回false,插入将被取消。
afterInsert - 在对象持久化到数据库之后执行
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 - 在更新对象之前执行。如果返回false,更新将被取消。
afterUpdate - 在对象更新后执行
class Person {
def securityService
String firstName
String lastName
String lastUpdatedBy
static constraints = {
lastUpdatedBy nullable: true
}
def beforeUpdate() {
lastUpdatedBy = securityService.currentAuthenticatedUsername()
}
}
=================================================================================================
3、beforeDelete - 在删除对象之前执行。如果返回false,删除将被取消。
afterDelete - 在对象被删除后执行
class Person {
String name
def beforeDelete() {
ActivityTrace.withNewSession {
new ActivityTrace(eventName: "Person Deleted", data: name).save()
}
}
}
=================================================================================================
4、beforeValidate - 在对象验证之前执行
class Person {
String name
static constraints = {
name size: 5..45
}
def beforeValidate() {
name = name?.trim()
}
}
=================================================================================================
5、onLoad - 从数据库加载对象时执行
class Person {
String name
Date dateCreated
Date lastUpdated
def onLoad() {
log.debug "Loading ${id}"
}
}
网友评论