gpt4 book ai didi

grails - 在 Grails 中,域类中是否有像 onLoad() 这样的东西?

转载 作者:行者123 更新时间:2023-12-02 06:22:08 29 4
gpt4 key购买 nike

伙计们,

我有以下域类:

class Product { 
String name,
String productRecord,

static transients = ['productRecord']
}

productRecord 是根据 Product 实例的 id 自动生成的字段。

所以我一直在想,有没有一个地方会在加载域实例时自动调用以生成productRecord 编号?

最好的方法是什么?

最佳答案

您可能可以利用 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


查看文档以获取一些示例。

关于grails - 在 Grails 中,域类中是否有像 onLoad() 这样的东西?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7453135/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com