我想在添加关系时减少到数据库的往返。
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();
有效,我只为孩子选择,为孩子更新。但它不适用于二级缓存。
=== 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 ===
如果我在Session 2中添加parent.Children.Add(child),NHibernate会选择父级,但为什么呢?我认为这是开销。
答案 0 :(得分:0)
好吧,当我在I.E.处理父母/子女关系时。类别我有一个unite类别,包含示例ID,Name和ParentID,使用它可以定义任何级别的父/子关系