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

时间:2015-02-04 23:19:48

标签: grails gorm

我正在使用Grails的库存管理系统,应用程序将跟踪用户,品牌,类型等资产信息。我有3个域类(其他类对此讨论不重要)即,用户移动广告资源

每个资产归特定用户所有。当资产从当前所有者转移到Movement类中的新所有者时,更改应反映在User类和Inventory类中。这些是我的课程:

用户:

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']

    static constraints = {
        userID blank: false, unique: true
        fullName blank: false
        position()
        department()

    }
} 

机芯:

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()对类User进行了一些更改,但是它出现了如下所示的错误。

用户类(编辑版):

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

0 个答案:

没有答案