vishnu
vishnu

Reputation: 587

Grails - beforeInsert throwing exception null id in entry

I am trying to use beforeInsert in my user domain class.

 class User {

    String reEnterPassword

    static constraints = {

        password(blank: false, nullable: false, size:5..50, validator: {password, obj ->
         def reEnterPassword = obj.properties['reEnterPassword']
         if(reEnterPassword == null) return true 
         reEnterPassword == password ? true : ['invalid.matchingpasswords']
         })
        reEnterPassword(bindable:true, blank: false);
    }


    def beforeInsert = {
        password = password.encodeAsSHA()
        }

    String toString(){
        name
        }


    static transients = ['reEnterPassword']

    }

in my controller i have save method ( generated)

    def save() {
    def userInstance = new User(params)
    if (!`userInstance.save(flush: true)`) {
        render(view: "create", model: [userInstance: userInstance])
        return
    }

This is throwing exception Grails runtime exception, org.hibernate.AssertionFailure: null id in entry (don't flush the Session after an exception occurs), when domain objects save method encounters a SQL Exception

I read in the documentation for auto timestamping that Do not attempt to flush the session within an event (such as with obj.save(flush:true)). Since events are fired during flushing this will cause a StackOverflowError.

In this case how to save my userInstance.save(flush: true) I tried to remove flush:true but still i am getting same error. if i remove flus:true..then when i need to call. When hibenate will flush all these records.

I tried the solution defined this JIRA ticket Please help me out. Thank you

Upvotes: 1

Views: 2216

Answers (3)

Urs
Urs

Reputation: 45

Can it be that you have other validation errors?

If you put your code in the beforeValidate method it will work:

def beforeValidate = {
     password = password.encodeAsSHA()
}

I guess I'm too late to help you, but I hope it helps others with the same issue.

Greetings, Urs

Upvotes: 1

ian
ian

Reputation: 89

I believe if the beforeInsert method return false then you get the "null id in entry" exception. Perhaps this is treated as an indication that validation has failed.

e.g. the following will cause the exception

def beforeInsert() {
  flag = false
}

however the following should work OK

def beforeInsert() {
  flag = false
  return true
}

Upvotes: 0

Jessica
Jessica

Reputation: 1

Change your

def beforeInsert = {
    password = password.encodeAsSHA()
}

to

def beforeInsert() {
    password = password.encodeAsSHA()
}

and that should do the trick

Upvotes: 0

Related Questions