Oskar, Thanks, I didn't think that NLog is required to get proper SQL output. sounds weird to me.
Since it's a Bag, shouldn't it skip getting the collection? It's simple insert after-all on a non-indexed collection... You say NH does it 'sometimes' if the collection is small? Also, how about the fact that if I am setting Inverse() on the collection, it makes the insert without retrieving the collection? On Saturday, December 29, 2012 12:16:35 PM UTC+2, Oskar Berggren wrote: > > 2012/12/27 Andrewz <[email protected] <javascript:>>: > > > 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 view this discussion on the web visit https://groups.google.com/d/msg/nhusers/-/h3HvcNuGHrwJ. 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.
