2012/12/27 Andrewz <[email protected]>: > var user = session.Get<User>(1); > var group = new Group(); > group.Users.Add(user); [1] > user.Group = group; [2]
To add to the discussion on the design issue, yes, line [1] is enough to persist the association when it's not inverse, but NHibernate won't update the already loaded user instance. If you load the user on a new session, it will show the user.Group correctly. However, to have a consistent state already in the original session, line [2] is needed. > Question #1: The SQL output I get in the console using the show_sql setting, > is this one: http://pastebin.com/Qiagzt6J > It does not show how the Group_id is set for the user, > only the group insert. > Why is that? Isn't SQL output showing everything? How to > see the complete SQL? There should be an UPDATE statement following the INSERT. Configure logging using log4net (and disable show_sql) to get more complete logs. You need to enable debug logs for at least "NHibernate.SQL". > Question #2: Next, I am adding a new user to an existing group > > var group = session.Load<Group>(1); > var user = new User(); > user.Group = group; > group.Users.Add(user); > > The output is this one: http://pastebin.com/5RF7Lzq2 > Why is it getting all users of the group in the 2nd statement? This is how it is. Often, collections aren't very large, and/or when one element is accessed it's likely that you will also access other elements. > I guess it's because of accessing the collection when calling > group.Users.Add(user), but why can't NHibernate see it's actually adding a > new user? For the indexed collections like list and map you can set lazy=extra to get this behavior. I find that when I expect a collection to be large, it can be a good alternative to avoid having the collection. Especially if the collection owner doesn't actually own the collection elements (i.e. does not control their lifetime). /Oskar -- You received this message because you are subscribed to the Google Groups "nhusers" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.
