0
votes

I want to reduce roundtrips to database when I add relationship.

public class Parent
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual IList Children { get; set; } //inverse = true; cascade = all
}

public class Child
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual Parent Parent { get; set; }
}

Child child = Session.Get(1);
Parent parent = Session.Load(1);
child.Parent = parent;
Session.Flush();

It works, I have only select for child and update for child. But it doesn't work with second level cache.

=== Session 1 ===
Parent parent = Session.Get(1);
var count = parent.Children.Count;
=== Session 1 ===

=== Session 2 ===
Child child = Session.Get(1);
Parent parent = Session.Load(1);
child.Parent = parent;
Session.Flush();
=== Session 2 ===

=== Session 3 ===
Parent parent = Session.Get(1);
var count = parent.Children.Count; //INCORRECT! Session 2 didn't update collection.
=== Session 3 ===

If I add parent.Children.Add(child) in Session 2, NHibernate do select for parent, but why? I think it's overhead.

1

1 Answers

0
votes

well, when I handle parent/child relationship on I.E. categories i have a unite category wich contains in example ID, Name and ParentID, using this you can define parent/child relationships to any level