StaleObjectstateException row was updated or deleted by

前端 未结 4 1612
遇见更好的自我
遇见更好的自我 2020-12-30 04:10

I am getting this exception in a controller of a web application based on spring framework using hibernate. I have tried many ways to counter this but could not resolve it.<

4条回答
  •  被撕碎了的回忆
    2020-12-30 04:40

    You're mis-using Hibernate in some way that causes it to think you're updating or deleting objects from the database.

    That's why calling flush() is throwing an exception.

    One possibility: you're incorrectly "sharing" Session or Entities, via member field(s) of your servlet or controller. This is the main reason 'synchronized' would change your error symptoms.. Short solution: don't ever do this. Sessions and Entities shouldn't & don't work this way -- each Request should get processed independently.

    Another possibility: unsaved-value defaults to 0 for "int" PK fields. You may be able to type these as "Integer" instead, if you really want to use 0 as a valid PK value.

    Third suggestion: use Hibernate Session explicitly, learn to write simple correct code that works, then load the Java source for Hibernate/ Spring libraries so you can read & understand what these libraries are actually doing for you.

提交回复
热议问题