Preface: This question is a derivative of this answer, speaking specifically about NHibernate instead of Hibernate.
As to Hibernate, javadoc to org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(EventSource) says:
Execute all SQL and second-level cache updates, in a special order so that foreign-key constraints cannot be violated:
- Inserts, in the order they were performed
- Updates Deletion of collection elements
- Insertion of collection elements
- Deletes, in the order they were performed
My Questions:
- If it applies to Hibernate, does this order apply to NHibernate too?
- Is this deterministic order documented somewhere?
Also – If this behavior is DBMS-specific, I’m using SQL Server.
The documentation of NHibernate 3.3 is in detail describing the steps when the Session.Flush is called (point 9.6). In case of
session.FlushMode = FlushMode.Commit;this is how the transaction batch will be executed:The steps are:
all entity insertions, in the same order the corresponding objects were saved using ISession.Save()
all entity updates
all collection deletions
all collection element deletions, updates and insertions
all collection insertions
all entity deletions, in the same order the corresponding objects were deleted using ISession.Delete()
I am mostly using SQL Server (2008) and can say that this is working this way..