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

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

提交回复
热议问题