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

前端 未结 14 1354
不思量自难忘°
不思量自难忘° 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:18

    what i have experienced is that this exception raise when updating object have an id which not exist in table. if you read exception message it says "Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1" which means it was unable to found record with your given id.

    To avoid this i always read record with same id if i found record back then i call update otherwise throw "exception record not found".

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

    its happen when you try to delete the same object and then again update the same object use this after delete

    session.clear();
    
    0 讨论(0)
  • 2021-01-01 11:20

    It looks like, cargo can have one or more item. Each item would have a reference to its corresponding cargo.

    From the log, item object is inserted first and then an attempt is made to update the cargo object (which does not exist).

    I guess what you actually want is cargo object to be created first and then the item object to be created with the id of the cargo object as the reference - so, essentally re-look at the save() method in the Action class.

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

    As mentioned above, be sure that you don't set any id fields which are supposed to be auto-generated.

    To cause this problem during testing, make sure that the db 'sees' aka flush this SQL, otherwise everything may seem fine when really its not.

    I encountered this problem when inserting my parent with a child into the db:

    1. Insert parent (with manual ID)
    2. Insert child (with autogenerated ID)
    3. Update foreign key in Child table to parent.

    The 3. statement failed. Indeed the entry with the autogenerated ID (by Hibernate) was not in the table as a trigger changed the ID upon each insertion, thus letting the update fail with no matching row found.

    Since the table can be updated without any Hibernate I added a check whether the ID is null and only fill it in then to the trigger.

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

    For update() and saveOrUpdate() methods, id generator value should be there in the database. For the save() method, id generator is not required.

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

    This often happens when your SQL is bad (implicit type conversions etc.).

    Turn on hibernate SQL logging by adding the following lines to your log4j properties file:

    logs the SQL statements

    log4j.logger.org.hibernate.SQL=debug

    Logs the JDBC parameters passed to a query

    log4j.logger.org.hibernate.type=trace

    Before failing you will see the last SQL statement attempted in your log, copy and paste this SQL into an external SQL client and run it.

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