【问题标题】:BeforeInsert and BeforeUpdate are fired twice in grails domain class在 grails 域类中,BeforeInsert 和 BeforeUpdate 被触发两次
【发布时间】:2015-03-10 10:40:20
【问题描述】:

我在我的应用程序中使用 grails 版本 2.1.0,并且我的域类中有一个 before insert,它对密码进行如下编码。

包 com.valuelabs.bets.security

类 SecUser { 瞬态springSecurityService

String username
String password
String emailId
String mobileNumber
String position
boolean enabled
boolean accountExpired
boolean accountLocked
boolean passwordExpired
boolean firstTimeLogin 
String userstatus
String userRole
Date activateDate
Date expireDate = new Date()
Audit audit
static embedded = ['audit']


static mapping = {
    password column: '`password`'
    sort "username"
}

def beforeInsert() {
    println "in before insert"
        encodePassword()
}

def beforeUpdate() {
    println "in before update"
    if (isDirty('password')) {
        encodePassword()
    }
    audit.lastUpdated = new Date()
}

protected void encodePassword() {
    println " Before ========================> "+ password
    if(springSecurityService){
        password = springSecurityService.encodePassword(password)
    }
    println " springSecurityService "+ springSecurityService +" password "+password
}


Set<SecRole> getAuthorities() {
    SecUserSecRole.findAllBySecUser(this).collect { it.secRole } as Set
}


String toString(){
    username
    }
static transients  = ['userstatus','userRole']

}

这是我的控制器逻辑

 if (!secUserInstance.save(flush:true)) {
        println "13"
        //secUserInstance.errors.allErrors.each { println it }
        render(view: "create", model: [secUserInstance: secUserInstance,curRole:""])
        return
    }

现在的问题是当我保存实例时密码被编码两次。

请让我知道grails版本是否有问题,如果有请告诉我正确的版本或可能的解决方案。

【问题讨论】:

  • 能否请您从控制器和服务中编写更多代码,您可以在其中编写保存代码并提供您的域结构。
  • 我已经更改了简述 domain,controller 的代码。请看一下
  • 当定义了多个数据源并且域类中包含datasource 'ALL' 时,我看到了相同的行为。即使它只插入一个数据源,它仍然会多次调用 beforeInsert。

标签: events grails persistence grails-domain-class


【解决方案1】:

您能否向我们展示您的 DataSource.groovy 文件?请注意,触发 GORM 事件的次数与应用程序中的数据源一样多。

【讨论】:

    猜你喜欢
    • 2012-03-06
    • 1970-01-01
    • 1970-01-01
    • 2018-08-06
    • 2015-03-13
    • 2019-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多