hibernate - StaleStateException:Hibernate尝试更新而不是插入吗?

标签 hibernate grails

我已经将以下域映射到mysql数据库:

class User {
  String username
  String passwordHash 

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

尝试执行实例的save()时,hibernate始终执行以下查询:

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



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

我已经尝试将flush:true更改为insert:true。没有效果。

最佳答案

这是我在上一个问题中遗漏给您的信息。如果要使用自己的标识符并分配自己的assigned,则必须使用generator作为id。所以:

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

此外,我建议将域名用作表Teamteam而不是User,否则我不确定该域名能否很好地发挥作用。

关于hibernate - StaleStateException:Hibernate尝试更新而不是插入吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18751985/

相关文章:

mysql - "org.hibernate.ObjectNotFoundException: No row with the given identifier exists"但它确实存在

mysql - spring boot和hibernate mysql数据库连接一直断开

grails - 无法在grails中调用方法本身

grails - Grails Spring Security:在双角色之间切换

java - Grails:防止外键内容被更新

grails - 保护 gsp 文件的安全

java - hibernate 一对一关系: Unable to delete the child table

java - 当表已经创建时,设置长度在 hibernate 中不起作用

date - g :formatDate alternative in a service

java - hibernate hbm2ddl.auto 默认值