org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1

前端 未结 14 1356
不思量自难忘°
不思量自难忘° 2021-01-01 10:56

I am using struts and hibernate. I have a parent and child relation using set in hbm. In the action I am using session.saveOrUpdate() method to save but while s

相关标签:
14条回答
  • 2021-01-01 11:25

    I had the same as well.Making the Id (0) doing "(your Model value).setId(0)" solved my problem.

    0 讨论(0)
  • 2021-01-01 11:25

    So For my case I noticed hibernate is trying to update the record rather than inserting it and that thrown the exception mentioned.

    I finally came to find that my entity had an updatedAt timestamp column:

    <timestamp name="updatedDate" column="updated_date" />
    

    and when I was trying to initialize the object i found that the code was setting this field explicitly.

    after removing that setUpdateDate(new Date()) it worked and did an insert instead.

    0 讨论(0)
  • 2021-01-01 11:25

    FYI, another way this exception can occur is if:

    • Your transaction isolation is READ_COMMITTED
    • Transaction #1 queries for an entity, then deletes that entity
    • A simultaneous transaction #2 does the same thing

    Then this can happen: TX #1 successfully commits before TX #2, then when TX #2 tries to delete the entity (again) it's not there any more - even though it was found by a query earlier in that same transaction. Note this anomaly is allowed with READ_COMMITTED isolation.

    In my case the resulting exception looked like this:

    HHH000315: Exception executing batch [org.hibernate.StaleStateException:
      Batch update returned unexpected row count from update [0]; actual row
      count: 0; expected: 1; statement executed: delete from Foobar where id=?],
      SQL: delete from Foobar where id=?
    
    0 讨论(0)
  • 2021-01-01 11:26
    /*
    * Thrown when a version number or timestamp check failed, indicating that the
    * Session contained stale data (when using long transactions with versioning). 
    * Also occurs if we try delete or update a row that does not exist.
    *
    */
    
    if ( expectedRowCount > rowCount ) {
       throw new StaleStateException(           
      "Batch update returned unexpected row count from update [" + batchPosition +"]; actual row count: " + rowCount +"; expected: " + expectedRowCount);
       }
    

    <property name="show_sql">true</property> This should show you the SQL that is executed and causes the problem.

    *The StaleStateException would be thrown only after we successfully deleted one object, and then tried to delete another. The reason for this is, while persisting the objects across sessions, objects must first be deleted from the Session before deleted. Otherwise, subsequent deletes will cause the StaleStateException to be thrown.

     Session.Remove(obj); 
     objectDAO.Delete(obj);
    

    *The problem was that a table must have only one field that is primary key (I had a composite key and this is not a good idea, except for the many to many relation). I have solved using a new id table field auto incremental.

    *It can be fix by using Hibernate session.update() -- you need to have the table/view's primary key equal your corresponding bean property (eg. id).

    *

    0 讨论(0)
  • 2021-01-01 11:28

    It looks like, when you try to delete the same object and then again update the same object, then it gives you this error. As after every update hibernate for safe checking fires how many rows were updated, but during the code the data must have been deleted. Over here hibernate distinguish between the objects based on the key what u have assigned or the equals method.

    so, just go through once through your code for this check, or try with implementing equals & hashcode method correctly which might help.

    0 讨论(0)
  • 2021-01-01 11:32

    In the Hibernate mapping file for the id property, if you use any generator class, for that property you should not set the value explicitly by using a setter method.

    If you set the value of the Id property explicitly, it will lead the error above. Check this to avoid this error.

    0 讨论(0)
提交回复
热议问题