Here's some sample code I have that replicates the problem I'm having (which sounds like the same issue).
Some background: the Instructor.courses relationship is defined as an indexed <list> in the hibernate mapping file, cascade is set to "all-delete-orphan". There are more than 2 related courses for the Instructor used in the test. This means that when a course is removed which is not the last one, the index columns for the higher courses are adjusted to fill in the gaps.
Code:
// load the object into memory
Session localSession = sessionFactory.openSession();
Transaction tx = localSession.beginTransaction();
Instructor localInstructor = new Instructor("Joe", "Blow");
localInstructor.getCourses().add(new Course("Math"));
localInstructor.getCourses().add(new Course("Science"));
localInstructor.getCourses().add(new Course("Reading"));
localSession.save(localInstructor);
tx.commit();
localSession.close();
// delete a related object (in reality, this is happening on a different machine)
Session remoteSession = sessionFactory.openSession();
Transaction remoteTx = remoteSession.beginTransaction();
Instructor remoteInstructor = (Instructor) remoteSession.load(Instructor.class, localInstructor.getId(), LockMode.UPGRADE_NOWAIT);
remoteInstructor.getCourses().remove(1);
remoteTx.commit();
remoteSession.close();
// now try deleting a related object on the local parent, whose 'children' relationship is out of sync
localSession = sessionFactory.openSession();
tx = localSession.beginTransaction();
localSession.lock(localInstructor, LockMode.UPGRADE_NOWAIT);
localInstructor.getCourses().remove(0);
tx.commit();
localSession.close();
The final call to commit() throws the following:
Code:
org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1
Due to this query:
Code:
Hibernate: update Course set instructorId=?, sortOrderForinstructor=? where id=?