Re: questions about sql logging and the Inverse in a one to many relationship

13 views
Skip to first unread message

Andrewz

unread,
Dec 23, 2012, 11:36:52 AM12/23/12
to fluent-n...@googlegroups.com
Anyone? I am a bit surprised by the lack of feedback

On Thursday, December 20, 2012 2:13:28 AM UTC+2, Andrewz wrote:
Hello,

I have a bi-directional one to many relationship between a User and a Group, defined as follows:


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/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/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



 


Jos van der Til

unread,
Dec 31, 2012, 12:03:58 PM12/31/12
to fluent-n...@googlegroups.com
I'm guessing this has to do with using session.Load<>() as opposed to session.Get<>() which you should be using in this situation (if you weren't doing group.Users.Add() then Load would have been fine).


P.S. This is not an question about FluentNHibernate but about NHibernate, try posting this in the nhusers group.
Reply all
Reply to author
Forward
0 new messages