Try calling Session.flush() between steps 4 and 5.
From the reference manual:
Quote:
From time to time the Session will execute the SQL statements needed to synchronize the JDBC connection's state with the state of objects held in memory. This process, flush, occurs by default at the following points
* from some invocations of find() or iterate()
* from net.sf.hibernate.Transaction.commit()
* from Session.flush()
The SQL statements are issued in the following order
1. all entity insertions, in the same order the corresponding objects were saved using Session.save()
2. all entity updates
3. all collection deletions
4. all collection element deletions, updates and insertions
5. all collection insertions
6. all entity deletions, in the same order the corresponding objects were deleted using Session.delete()
It could be that step 2 has been flushed to the database, but step 4 hasn't. Then steps 4 and 5 will be flushed together, and according to the documentation the INSERT will be done first.