簡體   English   中英

從BootStrap.groovy使用休眠事件創建域實例

[英]Creating a domain instance with a hibernate event from BootStrap.groovy

我試圖在我的BootStrap.groovy文件中創建一個用戶域對象,但收到以下錯誤:

[ERROR]:AssertionFailure  an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)

域對象如下所示:從afterInsert方法中調用服務時會發生問題。 該服務為非null,並且在其上調用任何方法(包括toString()或inspect())似乎都會導致錯誤。

BootStrap.groovy

def newUser = new User(...)
newUser.save(flush:true, failOnError: true)    

User.groovy

class User extends Auth {
    transient def userService

    ...

    def afterInsert() {
        log.debug "SERVICE: ${userService == null ? 'NULL': 'NOT NULL'}" // Gives: SERVICE: NOT NULL

        // Either of the following lines cause the error when uncommented
        //log.debug "SERVICE: ${userService.toString()}"
        //userService?.makeUser(this)
    }

}

是否應該使用BootStrap做到這一點,還是我有根本上的錯誤?

另外,從BootStrap調用時是否可以忽略此代碼? 例如,類似於:

def afterInsert() {
    if (notBootStrap()) {
        ...
    }
}

任何輸入將不勝感激!

Service需要transaction

def afterInsert() {
        User.withTransaction{
           userService.makeUser(this)
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM