Grails 如何在GORM中使用beforeInsert()和beforeUpdate()

Grails 如何在GORM中使用beforeInsert()和beforeUpdate(),grails,gorm,Grails,Gorm,我正在Grails中开发一个库存管理系统,该应用程序将跟踪用户、品牌、类型等资产信息。我有3个域类(其他类对本次讨论不重要),即,User、Movement和inventory 每个资产都由特定用户拥有。当资产从当前所有者转移到移动类中的新所有者时,更改应反映在用户类和库存类中。这些是我的课程: 用户: 运动: class Movement { User oldUser User newUser Inventory inventoryID Date moveme

我正在Grails中开发一个库存管理系统,该应用程序将跟踪用户、品牌、类型等资产信息。我有3个域类(其他类对本次讨论不重要),即,UserMovementinventory

每个资产都由特定用户拥有。当资产从当前所有者转移到移动类中的新所有者时,更改应反映在用户类和库存类中。这些是我的课程:

用户:

运动:

class Movement {

    User oldUser
    User newUser
    Inventory inventoryID
    Date movementDate
    User userResponsible

    static constraints = {
    inventoryID blank: false
    oldUser blank: false
    newUser blank: false
    movementDate()
    userResponsible blank: false
    }
}
库存:

class Inventory {

    String inventoryID
    String code
    String description
    String serial_num
    Date purchase_date
    Date record_date
    Type type 
    Brand brand 
    User user 


    static hasMany = [movements: Movement]

    String toString(){
        "$inventoryID, $type"
    }

    static constraints = {
        inventoryID blank: false, unique: true
        code blank: false
        description nullable: true, maxSize: 1000
        serial_num blank: false
        purchase_date()
        image nullable: true, maxSize: 1000000
        record_date()
        remarks nullable: true, maxSize: 1000
        type()
        brand()
        user()
    }
}
我听说过名为beforeInsert()和beforeUpdate()的GORM方法,它们可以完成这项工作,但我对Grails、Groovy和GORM完全陌生,因为这是我开发的第一个应用程序,我不知道如何在代码中使用它们。我做了大量的研究,但没有一项与我的设想相符,因此我无法清楚地了解如何使用它

从此处编辑的问题: 我使用beforeUpdate()和beforeUpdate()对类用户进行了一些更改,但它得到了如下所示的错误

用户类(编辑版本):

错误:

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00000000543e4bc2, pid=7488, tid=6520
#
# JRE version: Java(TM) SE Runtime Environment (8.0_05-b13) (build 1.8.0_05-b13)

# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.5-b02 mixed mode windows-amd64compressed oops)
# Problematic frame:
# V  [jvm.dll+0x424bc2]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# C:\Users\Kanaan\MFEDinventory\hs_err_pid7488.log
#
# Compiler replay data is saved as:
# C:\Users\Kanaan\MFEDinventory\replay_pid7488.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.sun.com/bugreport/crash.jsp
#
| Error Forked Grails VM exited with error

有一个非常清楚的例子解释,我做了一些更改,但后来我遇到了一个错误,我做得对还是错…?你得到了什么错误???@Abdullah上面显示的错误,我不知道它是与用户类相关还是由其他东西生成的。
class User {

    String userID
    String fullName
    String position
    Department department 

    String toString(){
        fullName
    }

    static hasMany = [inventories: Inventory, movementsByOldUser: Movement, movementsByNewUser: Movement]
    static mappedBy = [movementsByOldUser: 'oldUser', movementsByNewUser: 'newUser']

    def beforeInsert(){
        this.fullName = 'oldUser'
    }

    def beforeUpdate(){
        this.fullName = 'newUser'
    }

    static constraints = {
        userID blank: false, unique: true
        fullName blank: false
        position()
        department()
    }
}
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00000000543e4bc2, pid=7488, tid=6520
#
# JRE version: Java(TM) SE Runtime Environment (8.0_05-b13) (build 1.8.0_05-b13)

# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.5-b02 mixed mode windows-amd64compressed oops)
# Problematic frame:
# V  [jvm.dll+0x424bc2]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# C:\Users\Kanaan\MFEDinventory\hs_err_pid7488.log
#
# Compiler replay data is saved as:
# C:\Users\Kanaan\MFEDinventory\replay_pid7488.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.sun.com/bugreport/crash.jsp
#
| Error Forked Grails VM exited with error