0
當我添加關係時,我想減少往返數據庫。處理父/子的最有效方法
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 ===
如果我在會話2中添加parent.Children.Add(child),NHibernate會爲父類選擇,但爲什麼?我認爲這是開銷。
你是什麼意思? child.parentId = parentId?它也不適用於二級緩存。 – Andy 2010-12-17 09:26:43