This is a cross-post from StackOverflow, but doing it only because the 
original question did not get much attention (or answers). The original 
post can be found here 
<http://stackoverflow.com/questions/25642429/nhibernate-saving-collection-elements-before-parent>;
 
the gist is summarized below.

According to NHibernate documentation, SQL statements are issued in the 
following order:

   1. all entity insertions, in the same order the corresponding objects 
   were saved using ISession.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 ISession.Delete()
   
We have two entities, *Client* and *Profile*, where *Client* owns and is 
solely responsible for its child collection of *Profiles*. *Client* makes 
use of versioning. Via session interceptors we ensure that its version 
field is marked as dirty whenever its child collection changes. According 
to the above order, when a new *Profile* is added to *Client* we should see 
an SQL UPDATE for *Client* changing the version, and then an SQL INSERT for 
the new *Profile*. However, the following actually happens as observed via 
ExpressProfiler:

SELECT Id, Version 
  FROM Clients 
  WHERE Id='3F08F098-09C8-CE42-9594-39C9EAA21B64'
SELECT ClientId, ProductId, Score, Comment 
  FROM ClientProfiles 
  WHERE ClientId='3F08F098-09C8-CE42-9594-39C9EAA21B64'
*INSERT INTO ClientProfiles* (Comment, Score, ClientId, ProductId) 
  VALUES ('some comment', 123, '3F08F098-09C8-CE42-9594-39C9EAA21B64', 
'487BC856-6EAF-42E2-ADD6-B3DE056EA714')
*UPDATE Clients SET Version = 46* WHERE Id = 
'3F08F098-09C8-CE42-9594-39C9EAA21B64' AND Version = 45

The fact that the INSERT happens before the UPDATE is undesirable in our 
case as the infrastructure expects a StaleObjectStateException in case 
optimistic concurrency finds two processes modifying the same data. We 
don't really see why the order does not seem to follow documentation. Hence 
hoping that NH masters will have more ideas as to why this is happening! 
Will be grateful for any and all input that brings us a little closer to a 
solution.

(Class definitions and mappings can be found on StackOverflow.)

Regards,
Dawid Ciecierski

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.

Reply via email to