I don't understand why would NH think that even if the collection is small, it's fine to get them from database, when it's just needs to insert a new object
I am not sure, but isn't NH overriding the Add method? Again, I am not sure, I am just asking. Thanks, Andrei On Saturday, December 29, 2012 4:18:44 PM UTC+2, Oskar Berggren wrote: > > 2012/12/29 Andrewz <[email protected] <javascript:>>: > > Oskar, > > > > Thanks, I didn't think that NLog is required to get proper SQL output. > > sounds weird to me. > > If you think some SQL is missing from "show_sql" output, please > research the problem in the NH code base and submit an issue report in > Jira. > > > > 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? > > I mean that often collections aren't very large, in which case loading > the entire contents is not a problem. Also, often multiple elements > will be accessed, in which case it might actually be faster to load > the whole collection in one go. > > For the cases where the collection is _expected by the developer_ to > be large, one can use lazy=extra, or avoid the collection completely > (and use direct querying to get the needed elements instead). > > > > > > Also, how about the fact that if I am setting Inverse() on the > collection, > > it makes the insert without retrieving the collection? > > Currently, calling Add() on a persistent bag will load the collection > if IsOperationQueueEnabled returns false: > > protected bool IsOperationQueueEnabled > { > get { return !initialized && IsConnectedToSession && > IsInverseCollection; } > } > > > /Oskar > > > > > > > > On Saturday, December 29, 2012 12:16:35 PM UTC+2, Oskar Berggren wrote: > >> > >> 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 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]<javascript:>. > > > To unsubscribe from this group, send email to > > [email protected] <javascript:>. > > For more options, visit this group at > > http://groups.google.com/group/nhusers?hl=en. > -- 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/-/PxAS0uoVXdMJ. 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.
