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

前端 未结 14 1497
不思量自难忘°
不思量自难忘° 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: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);
       }
    

    true 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).

    *

提交回复
热议问题