Search code examples
mongodbgrailsgrails-plugingrails-2.0grails-controller

Cannot obtain DBObject for transient instance, save a valid instance first


Cannot obtain DBObject for transient instance, save a valid instance first

I am using MongoDb plugins in Grails 2.2.2

when i am creating a new user.. its saves but.. when i trying to edit telephone number its gives me an error

when i am trying to update my telephone number

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

Controller Code

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)
    }

Model User :

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


    }

}

Solution

  • this part is needed .. when you using mongoDb.. in our model part

    removed static hasMany = [telephone:Core]

    and added

    static mapping = { telephone cascade: 'all-delete-orphan' }