If for instance you had a set or other constrained collection, you need to know what is in it to validate the insert. But that's not the reason it gets fetched. That happens because you told nh to do it, implicitly.
It's probably worth spending more time getting comfortable with how the system works, and then coming back to questions about why it does things in a particular fashion. On Dec 28, 2012 9:13 PM, "Andrewz" <[email protected]> wrote: > Hi, > > Thanks for your response. > > The question #1: Since the Inverse is not set, the theory is that > NHibernate sets the Group_Id each of for the User automatically. > Which is correct, after I run the code the Group_Id is indeed set in the > database. > But I cannot see how Group_Id is set in the SQL output: > http://pastebin.com/Qiagzt6J > There is no insert with a Group_Id value set. > > The question #2: yes, it's getting the collection from database as you > mentioned, but my question is why does it have to bring the collection in > order to perform the insert? > Like I said, if I am setting the Inverse, it inserts without the bringing > the collection. > > Thanks, > Andrew > > > On Saturday, December 29, 2012 2:54:38 AM UTC+2, SexualChocolate wrote: >> >> Hi, >> >> I'm not sure I understand question one precisely, but as for number two- >> >> When you do group.users.add, the users object is being lazy loaded from >> the database. That creates the select you're seeing. >> On Dec 27, 2012 5:47 PM, "Andrewz" <[email protected]> wrote: >> >>> Hello, >>> >>> I have a bi-directional one to many relationship between a User and a >>> Group, defined as follows: >>> >>> http://pastebin.com/qxYZc4jV >>> >>> Note that the Inverse is not set. Based on my understanding this means >>> that NHibernate sets the Group_Id each of for the User automatically >>> >>> Here is my program, adding a new group and assigning an existing user to >>> this group: >>> >>> var user = session.Get<User>(1); >>> var group = new Group(); >>> group.Users.Add(user); >>> user.Group = group; >>> session.Save(user); >>> >>> Question #1: The SQL output I get in the console using the show_sql >>> setting, is this one: >>> http://pastebin.com/**Qiagz**t6J<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? >>> >>> 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/**5RF7L**zq2<http://pastebin.com/5RF7Lzq2> >>> Why is it getting all users of the group in the 2nd statement? >>> 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? >>> >>> The weird thing is that if I am setting Inverse() on the collection, it >>> makes the insert without retrieving the collection, which is how it should >>> work. >>> >>> I know that in practice, setting Inverse is almost always needed(you >>> want to have non null restriction on the Group_id not null, and without >>> Inverse, this is not possible because the insert will fail), but I am >>> really trying to understand what is going on. >>> >>> Thanks! >>> Andrew >>> >>> -- >>> 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/-/sXSKABVMWQEJ<https://groups.google.com/d/msg/nhusers/-/sXSKABVMWQEJ> >>> . >>> To post to this group, send email to [email protected]. >>> To unsubscribe from this group, send email to nhusers+u...@** >>> googlegroups.com. >>> For more options, visit this group at http://groups.google.com/** >>> group/nhusers?hl=en <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/-/-51pzlQNyZkJ. > 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. > -- 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.
