我有
public class Parents {
public Parents() {
Childs = new HashedSet<Childs>();
}
public virtual int Id {get; set;}
public virtual String Name {get; set;}
public virtual ISet<Childs> Childs {get; protected set;}
}
public class Childs {
public virtual int Id {get; set;}
public virtual String Name {get; set;}
public virtual Parents Parent {get; set;}
}
映射
....
.Override<Parents>(map => {
map.HasMany(x => x.Childs).KeyColumn("parent_id").Cascade.SaveUpdate().Not.LazyLoad().AsSet().Fetch().Join();
})
.Override<Childs>(map => {
map.References(x => x.Parent, "parent_id");
})
....
如何使用 Link、HQL 或 AutoMapper 获取(选择)有孩子的父母?例如,我的测试用例中有这样的查询,
Parents parent = new Parents {Name = "parent test"};
Childs child = new Childs {Name = "child test", Parent = parent};
session.Save(parent);
session.Save(child);
...
var myParent = session.QueryOver<Parents>().Where(x=>x.Id==1).List()[0];
Assert.IsTrue(myParent.Childs.Count>0);
...
在日志中我看到查询:选择加入,但我只得到没有孩子的父母