malte
malte

Reputation: 1181

StaleStateException: Hibernate tries update instead of insert?

I've got the following domain mapped to a mysql-database:

class User {
  String username
  String passwordHash 

static mapping={
    table 'team'
    version false
    passwordHash column: 'Password' 
    id column: 'username', name: 'username'
   }
}

When trying to execute the save() of an instance, hibernate always executes this query:

Hibernate: update team set Password=? where username=?

calling of save():

def save(){
   def userInstance = new User()
   userInstance.username = "test"
   userInstance.passwordHash = "abcd"
   if(!userInstance.save(flush:true)){
       //
   }
}

I already tried to change flush:true to insert:true. No effect.

Upvotes: 1

Views: 641

Answers (1)

dmahapatro
dmahapatro

Reputation: 50245

This is something I missed to inform you in the last question. You have to use generator as assigned if you are using your own identifier and assigning your own id. So:

id column: 'username', name: 'username', generator: 'assigned'

Moreover I would suggest to use the domain name as Team for table team instead of User, I am not sure that will play well otherwise.

Upvotes: 2

Related Questions