BeforeInsert和BeforeUpdate在grails域类中触发两次

时间:2015-03-10 10:40:21

标签: events grails persistence grails-domain-class

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

package com.valuelabs.bets.security

类Sec​​User {    transient 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版本是否有任何问题,如果有的话请告诉我正确的版本或可能的解决方案。

1 个答案:

答案 0 :(得分:1)

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