开发者

Most efficient way to handle parent/child

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 do开发者_运维技巧esn'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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜