Sedat Başar
Sedat Başar

Reputation: 3798

GORM - getting sql exception at .get() function

hi i am using GORM and i have an user table in database. also I have an update method that updates a users profile. when i am trying to update a contact its ok with first try, but after second or sometimes third try, iam getting

ERROR org.hibernate.transaction.JDBCTransaction - Could not toggle autocommit
java.sql.SQLException: Error during query: Unexpected Exception: java.lang.ArrayIndexOutOfBoundsException message given: 3

exception when i am trying to do this:

User updateUser(User tempUser){
    def id=tempUser.id
    User user = User.get(id)//this line throws exception

Upvotes: 0

Views: 521

Answers (1)

Vladimir
Vladimir

Reputation: 517

First of all, there is not enough information.

User updateUser (User tempUser) {
    def id=tempUser.id
    User user = User.get(id)//this line throws exception

Why are you doing it like this? Is this code in a Domain class?

You can update User instance classic way in your controller:

def userInstance = User.get(params.id)
if(userInstance) {
    userInstance.properties = params
    if (!userInstance.hasErrors() && userInstance.save(flush: true)) {
        flash.message = "User was updated successfully"
        // redirect somewhere
    }
}

Also you can try to add method to user instance (in User domain class), smth like this:

Class User {
    [...]

    def updateUserInstance(params) {
        it.properties = params
        if (!it.hasErrors() && it.save(flush: true)) {
            return true // or return it
        } else {
            return false
        }
    }
}

and then invoke it in your controller:

def userInstance = User.get(params.id)
if(userInstance.updateUserInstance(params)) {
    // do something
}

I didn't test code snippets above, so be carefull. And about your code, it would be nice, if you gave some more code: parts of User class, more of updateUser method.

Regards.

Upvotes: 1

Related Questions