When manipulating the data in-memory, you are responsible for managing both sides of the relationship. You can do this easily with an Add
method in ProjectData
:
public virtual void Add(BranchData branch)
{
branch.ProjectData = this;
Branches.Add(branch);
}
On the other side, NHibernate is responsible for properly hydrating and dehydrating your objects. However, you aren't seeing this because your queries are all occurring within the same session. Consider this code:
var project = new ProjectData();
session.Save(project);
session.Flush();
var freshProject = session.Get<ProjectData>(project.Id);
if (ReferenceEquals(project, freshProject))
Console.WriteLine("They're the same instance.");
This code will display "They're the same instance." That instance will still have the relationships set up exactly as you initialized them, correct or not.
If you neglect to set the relationship on the inverse side, project.Branches
, NHibernate will still be able to save it fine, but it will still continue to look inconsistent for the duration of that session. If you close the session and open a new one (this might be problematic for your in-memory test), or just call session.Clear()
or session.Evict(project)
, then you will see data in project.Branches
when you load it again.