0

临时实例无法获取 DBObject,请先保存一个有效实例

我在 Grails 2.2.2 中使用 MongoDb 插件

当我创建一个新用户时..它的保存但是..当我尝试编辑电话号码时它给了我一个错误

当我尝试更新我的电话号码时

<g:textField name="user.telephone[0].telephone_number" value="${userProfileInstance?.user.telephone[0]?.telephone_number}" class="loginTxtBox" placeholder="Mobile" />

控制器代码

def editProfile(Long id) {
        def userProfileInstance=user.get(id)
        if (!userProfileInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'user'), id])
            redirect(action: "list")
            return
        }
        [userProfileInstance:userProfileInstance]
    }



    def update(Long id, Long version) {
        def userProfileInstance=user.get(id)
        if (!userProfileInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'user'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (userProfileInstance.version > version) {
               userProfileInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'user.label', default: 'user')] as Object[],
                          "Another user has updated thisuser while you were editing")
                render(view: "edit", model: [userProfileInstance:userProfileInstance])
                return
            }
        }

       userProfileInstance.properties = params

        if (!userProfileInstance.save(flush: true)) {
            render(view: "edit", model: [userProfileInstance:userProfileInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'user.label', default: 'user'),userProfileInstance.id])
        redirect(action: "show", id:userProfileInstance.id)
    }

模型用户:

package talent

import org.apache.commons.collections.ListUtils


class User {
    static mapWith = "mongo"


    String firstname;

    String lastname;

    String email;

    String password;

    String address1;

    List<Telephone> telephone = ListUtils.lazyList(new ArrayList(), {new Telephone()} as org.apache.commons.collections.Factory)

    static hasMany = [telephone:Core]

    static embedded =['telephone']



    static constraints = {      

        telephone blank: false, nullable: false


    }

}
4

1 回答 1

0

这部分是需要的..当您在我们的模型部分中使用 mongoDb..

删除静态 hasMany = [电话:核心]

并添加

静态映射 = {电话级联:'all-delete-orphan'}

于 2013-05-24T11:08:06.783 回答