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
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".
its happen when you try to delete the same object and then again update the same object use this after delete
session.clear();
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.
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:
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.
For update()
and saveOrUpdate()
methods, id
generator value should be there in the database. For the save()
method, id
generator is not required.
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:
log4j.logger.org.hibernate.SQL=debug
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.