无法获取瞬态实例的DBObject,先保存有效实例

时间:2013-05-24 07:50:06

标签: mongodb grails grails-plugin grails-2.0 grails-controller

无法获取瞬态实例的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


    }

}

1 个答案:

答案 0 :(得分:0)

当你在我们的模型部分使用mongoDb ..时需要这个部分

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

并添加了

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