Here is my class:
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public ISet<User> Friends { get; set; }
}
Here is my mapping:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="Test" assembly="test">
<class name="User" table="Users">
<id name="Id" column="id">
<generator class="native"/>
</id>
<property name="Name" column="name"/>
<set name="Friends" table="Friends">
<key column="user_id"/>
<many-to-many class="User" column="friend_id"/>
</set>
</class>
</hibernate-mapping>
Here is the problem:
User user = session.Load<User>(1);
User friend = new User();
friend.Name = "new friend";
user.Friends.Add(friend);
At the last line [user.Friends.Add(friend)], I noticed that it will initialize the Friends collection before add new friend to it.
My question is: Is there anyway to avoid this behavior in NHibernate? Because I just want to have only single INSERT command to be executed for performance reason.