Grails——GORM中的事件

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}"
}
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,282评论 19 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 33,776评论 18 399
  • //Clojure入门教程: Clojure – Functional Programming for the J...
    葡萄喃喃呓语阅读 9,304评论 0 7
  • 我有点懂了,为什么自以为经历过一些极其难挨的日子和时刻,本该方寸都难忘,可实际上只剩下很清浅的记忆,脑子里也只淡...
    浅阿草阅读 2,435评论 0 0
  • 9月2号,得到APP发布了每天听本书的2.0版本。简单的说,一年365天,300多块钱,听300多本书(甚至更多)...
    Zenith_Swimmer阅读 1,679评论 3 0

友情链接更多精彩内容