Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)

前端 未结 17 1892
别跟我提以往
别跟我提以往 2020-12-02 16:36

I have a java project that runs on a webserver. I always hit this exception.

I read some documentation, and found that pessimistic locking (or optimistic, but I read

17条回答
  •  醉酒成梦
    2020-12-02 16:59

    This exception is probably caused by optimistic locking (or by a bug in your code). You're probably using it without knowing. And your pseudo-code (which should be replaced by real code to be able to diagnose the problem) is wrong. Hibernate saves all the modifications done to attached entities automatically. You shouldn't ever call update, merge or saveOrUpdate on an attached entity. Just do

    Email email = session.get(emailId);
    email.setSubject(subject);
    

    No need to call update. Hibernate will flush the changes automatically before committing the transaction.

提交回复
热议问题