我正在使用带有默认 h2 数据库的 grails 2.0.3 并具有以下用户域类:
class User {
transient springSecurityService
String username
String password
boolean enabled
boolean accountExpired
boolean accountLocked
boolean passwordExpired
Preferences preferences
Company company
Personal personal
static constraints = {
username email: true, blank: false, unique: true
password blank: false
preferences unique: true
company unique: true
personal unique: true
}
static mapping = {
password column: '`password`'
}
Set<Role> getAuthorities() {
UserRole.findAllByUser(this).collect { it.role } as Set
}
def beforeInsert() {
encodePassword()
}
def beforeUpdate() {
if (isDirty('password')) {
encodePassword()
}
}
protected void encodePassword() {
password = springSecurityService.encodePassword(password)
}
}
在控制器中,我使用以下代码保存用户:
userInstance.save(flush: true)
现在,今天下午,我意识到密码字段应该有一个大小约束,因此修改了域类,使其如下(仅更改在约束中):
class User {
transient springSecurityService
String username
String password
boolean enabled
boolean accountExpired
boolean accountLocked
boolean passwordExpired
Preferences preferences
Company company
Personal personal
static constraints = {
username email: true, blank: false, unique: true
password blank: false, size: 6..15
preferences unique: true
company unique: true
personal unique: true
}
static mapping = {
password column: '`password`'
}
Set<Role> getAuthorities() {
UserRole.findAllByUser(this).collect { it.role } as Set
}
def beforeInsert() {
encodePassword()
}
def beforeUpdate() {
if (isDirty('password')) {
encodePassword()
}
}
protected void encodePassword() {
password = springSecurityService.encodePassword(password)
}
}
随后我再次生成了视图和控制器。现在,当我尝试从控制器中保存用户对象时,使用:
userInstance.save(flush: true)
我收到以下异常:
类:org.hibernate.AssertionFailure 消息:login.User 条目中的空 id(发生异常后不要刷新 Session)
任何帮助将不胜感激。
信息:如果我从新的/修改的类中删除大小约束,保存会很好。