【发布时间】:2015-07-22 22:37:19
【问题描述】:
当我尝试更新与 Profile 对象具有 hasOne 关联的用户域模型时,我似乎无法理解当前哪里出错了。
我的领域模型如下:
class User {
static hasOne = [profile: Profile]
static fetchMode = [profile: 'eager']
ObjectId id
String username
}
class Profile {
static belongsTo = [user: User]
ObjectId id
String familyName
String givenName
}
我最初可以使用个人资料来持久化用户,但在尝试更新用户对象时出现验证错误。
Validation error occurred during call to save():
- Field error in object 'co.suitable.User' on field 'profile.familyName': rejected value [null]
- Field error in object 'co.suitable.User' on field 'profile.givenName': rejected value [null]
我可以在保存对象之前打印出 user.profile ID 和 user.profile.familyName。像下面这样:
println(user.profile.familyName)
println(user.profile.id.toString())
user.save(flush: true, failOnError: true)
但在保存之前我仍然会收到验证错误,我想println(user.profile.familyName) 调用正在获取配置文件对象,如果它尚未加载,我认为设置 fetchMode 会处理。
当我这样做时,对象能够成功持久化并保存:
user.profile = Profile.findById(user.profile.id)
println(user.profile.id.toString())
user.save(flush: true, failOnError: true)
我可以将它包装在一个服务中,但我希望如果可能的话,我希望有一个由 Grails 处理的解决方案。非常感谢任何建议或想法。
【问题讨论】: