在Grails中是否有像onLoad()这样的域类? [英] In Grails is there something in domain class like onLoad()?
问题描述
伙计,
我有一个以下的域类:
class Product {
String name,
String productRecord,
static transients = ['productRecord']
}
productRecord是根据Product实例的id自动生成的字段。
所以我一直在想,是否有一个地方会在域实例加载时产生productRecord数字时自动调用?
最好的方法是做什么?
解决方案您可以利用内置域名事件:
GORM支持将事件注册为某些事件发生时触发
的方法,如删除,inser ts和更新。
下面是一个支持事件的列表:
- beforeInsert - 在一个对象最初持续到
之前执行数据库
- beforeUpdate - 更新对象前执行
- beforeDelete - 删除对象前执行
- beforeValidate - 在对象验证前执行
- afterInsert - 将对象持久保存到数据库后执行
- afterUpdate - 更新对象后执行
- afterDelete - 在删除对象后执行
- onLoad - 从数据库加载对象时执行
查看一些例子的文档。
Guys,
I have a following domain class:
class Product { String name, String productRecord, static transients = ['productRecord'] }
productRecord is a field which is generated automatically based on the id of the Product instance.
So I've been thinking, is there a place which will be automatically called when a domain instance is load to generate the productRecord number?
What's the best way to do that?
解决方案You can probably leverage the built-in Domain Events:
GORM supports the registration of events as methods that get fired when certain events occurs such as deletes, inserts and updates. The following is a list of supported events:
- beforeInsert - Executed before an object is initially persisted to the database
- beforeUpdate - Executed before an object is updated
- beforeDelete - Executed before an object is deleted
- 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
Have a look at the docs for some examples.
这篇关于在Grails中是否有像onLoad()这样的域类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!