Grails save()失败;无法弄清楚为什么

时间:2011-09-13 15:20:34

标签: grails groovy

我在保存域对象时遇到问题。以下是我的控制器代码:

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实例为null;然而,这不可能是真的(见下文)

This Contact: org.icc.callrz.User.User : 2
This User: org.icc.callrz.User.User : 1

org.icc.callrz.Contact.Contact中的字段'user'是:

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]

编辑:使用'generate-all'代码创建Contact域对象没有问题。

解决方案:我查看了视图中的代码,看起来好像创建了ID,所以我将代码更改为:

thisContact.user.id = request.user.id

然而,我收到了一个错误:java.lang.NullPointerException: Cannot set property 'id' on null objectprintln request.user的输出并非空白,所以我不确定为什么会出现这种情况。

然后我将违规行更改为:

thisContact.user = request.user

现在一切正常。 :)

1 个答案:

答案 0 :(得分:1)

尝试更换:

thisContact.setBelongsTo(request.user)

使用:

thisContact.user = thisUser

据我所知,您使用的语法是错误的,更不用说您构造thisUser而然后继续使用request.user。