我在保存域对象时遇到问题。下面是我的 Controller 代码:
def onContactRequest = {
if(request.method == 'POST') {
if(User.findByUserTelephone(params.userTelephone)) {
User thisUser = User.findByUserTelephone(params.userTelephone)
Contact thisContact = new Contact()
thisContact.setContact(thisUser)
println("This Contact: " + thisContact.getContact());
thisContact.setBelongsTo(request.user)
println("This User: " + request.user)
if(thisContact.save(flush: true)) {
render(thisContact.belongsTo.userName + " just requested " + thisContact.getContact().userName )
} else {
render("There was a problem saving the Contact.")
if( !thisContact.save() ) {
thisContact.errors.each {
println it
}
}
}
} else {
User thisUser = new User()
thisUser.setUserName("Not Set")
thisUser.setUserTelephone(params.userTelephone)
thisUser.save()
Contact thisContact = new Contact()
thisContact.setContact(thisUser)
thisContact.setBelongsTo(request.user)
if(thisContact.save(flush: true)) {
render(thisContact.belongsTo.userName + " just requested " + thisContact.getContact().userName )
} else {
render("There was a problem saving the Contact.")
if( !thisContact.save() ) {
thisContact.errors.each {
println it + "\n"
}
}
}
}
} else {
}
使用以下代码打印错误消息;因此它非常难看:
if( !thisContact.save() ) {
thisContact.errors.each {
println it + "\n"
}
}
据我所知,它提示 Contact 或 User 实例为空;但这不可能是真的(见下文)
This Contact: org.icc.callrz.User.User : 2
This User: org.icc.callrz.User.User : 1
org.icc.callrz.Contact.Contact 中的字段“用户”是:
static belongsTo = [
user: User
]
错误详情如下:
org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'org.icc.callrz.Contact.Contact' on field 'user': rejected value [null]; codes [org.icc.callrz.Contact.Contact.user.nullable.error.org.icc.callrz.Contact.Contact.user,org.icc.callrz.Contact.Contact.user.nullable.error.user,org.icc.callrz.Contact.Contact.user.nullable.error.org.icc.callrz.User.User,org.icc.callrz.Contact.Contact.user.nullable.error,contact.user.nullable.error.org.icc.callrz.Contact.Contact.user,contact.user.nullable.error.user,contact.user.nullable.error.org.icc.callrz.User.User,contact.user.nullable.error,org.icc.callrz.Contact.Contact.user.nullable.org.icc.callrz.Contact.Contact.user,org.icc.callrz.Contact.Contact.user.nullable.user,org.icc.callrz.Contact.Contact.user.nullable.org.icc.callrz.User.User,org.icc.callrz.Contact.Contact.user.nullable,contact.user.nullable.org.icc.callrz.Contact.Contact.user,contact.user.nullable.user,contact.user.nullable.org.icc.callrz.User.User,contact.user.nullable,nullable.org.icc.callrz.Contact.Contact.user,nullable.user,nullable.org.icc.callrz.User.User,nullable]; arguments [user,class org.icc.callrz.Contact.Contact]; default message [Property [{0}] of class [{1}] cannot be null]
编辑:使用“全部生成”代码创建联系人域对象没有问题。
解决方案 : 看了下 View 中的代码,好像是用了创建ID,所以把代码改成:
thisContact.user.id = request.user.id
但是,然后我收到一个错误:
java.lang.NullPointerException: Cannot set property 'id' on null object
但是 println request.user
的输出不是空白,所以我不确定为什么会出现。然后我将违规行更改为:
thisContact.user = request.user
现在一切正常。 :)
最佳答案
尝试更换:
thisContact.setBelongsTo(request.user)
和:
thisContact.user = thisUser
据我所知,您使用的语法是错误的,更不用说您构造 thisUser 然后继续使用 request.user 。
关于Grails save() 失败;不知道为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7404359/