gpt4 book ai didi

grails - Grails 3:更新域由于违反唯一约束而失败

转载 作者:行者123 更新时间:2023-12-02 15:53:25 24 4
gpt4 key购买 nike

我有以下抽象域类。它是根据SpringSecurity的规范 UserDetailsS​​ervice 实现的:

abstract class Users {
transient passwordEncoder
String username
String password
boolean enabled = true

static constraints = {
username blank: false, unique: true
password blank: false
}

static mapping = {
tablePerHierarchy false
id generator: 'assigned', name: 'username'
version false
}

def beforeInsert() {
encodePassword()
}

def beforeUpdate() {
if (isDirty('password')) {
encodePassword()
}
}

protected void encodePassword() {
password = passwordEncoder ?
passwordEncoder.encode(password) : password
}
}

这是我用来实现它的 Realm 类:
@Resource(uri="/userinfo")
class UserInfo extends Users {
String name
String title
enum Status {
ACTIVE,
INACTIVE,
UNCONFIRMED
}

Status status = Status.UNCONFIRMED
static belongsTo = [organization: Organization]
static constraints = {
name blank: false
organization nullable: false
}
}

问题是我可以创建UserInfo类的实例,但不能更新它们。运行以下代码:
UserInfo userInfo = UserInfo.findByUsername "fakeemail@gmail.com"
userInfo.status = UserInfo.Status.ACTIVE
if(!userInfo.save()) {
logger.error "Unable to save user updates"
userInfo.errors.allErrors.each {
println it
}
}

导致错误:

Field error in object 'com.UserInfo' on field 'username': rejected value [fakeemail@gmail.com]; codes [com.UserInfo.username.unique.error.com.UserInfo.username,com.UserInfo.username.unique.error.username,com.UserInfo.username.unique.error.java.lang.String,com.UserInfo.username.unique.error,userInfo.username.unique.error.com.UserInfo.username,userInfo.username.unique.error.username,userInfo.username.unique.error.java.lang.String,userInfo.username.unique.error,com.UserInfo.username.unique.com.UserInfo.username,com.UserInfo.username.unique.username,com.UserInfo.username.unique.java.lang.String,com.UserInfo.username.unique,userInfo.username.unique.com.UserInfo.username,userInfo.username.unique.username,userInfo.username.unique.java.lang.String,userInfo.username.unique,unique.com.UserInfo.username,unique.username,unique.java.lang.String,unique]; arguments [username,class com.UserInfo,fakeemail@gmail.com]; default message [Property [{0}] of class [{1}] with value [{2}] must be unique]



我究竟做错了什么?

最佳答案

What am I doing wrong?



据我所知,什么都没有。看起来好像是验证支持中的错误。

关于grails - Grails 3:更新域由于违反唯一约束而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35678651/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com