我正在努力解决 NHibernate 映射问题。我正在使用 Repository/UnitOfWork 模式,我正在尝试通过更新方法级联持久化我的对象。例如:我可以更改 Foo,添加/更新/删除 Bar 对象,一切都可以。但是当我尝试将 Son 和 Daughter 对象添加到“Bar”并更新 Foo (以持久 Bar 及其子对象)时,Son 和 Daughter 对象不会持久保存到 DB,只有 Foo 和所有 Bars (没有 Son/Daughter 对象) .
public class Foo
{
public int FooID {get; set;}
public string Name {get; set;}
public virtual IList<Bar> Bars {get; set;}
public Foo(){}
}
public class Bar
{
public int BarID {get; set;}
public string Name {get; set;}
public Foo Foo {get; set;}
public virtual IList<Son> Sons {get; set;}
public virtual IList<Daughter> Daughters {get; set;}
public Bar(){}
}
public class Son
{
public int SonID {get; set;}
public string Name {get; set;}
public virtual Bar Bar {get; set;}
public Son(){}
}
public class Daughter
{
public int DaughterID {get; set;}
public string Name {get; set;}
public virtual Bar Bar {get; set;}
public Daughter(){}
}
//on Foo.hbm.xml I have:
...
<bag name="Bar" table="Bar" inverse="true" cascade="all-delete-orphan" lazy="false">
<key column="FooID" />
<one-to-many class="Bar" />
</bag>
//on Bar.hbm.xml I have
...
<many-to-one name="Foo" column="FooID" class="Foo" lazy="false" />
<bag name="Son" table="Son" inverse="true" cascade="all-delete-orphan" lazy="false">
<key column="SonID" />
<one-to-many class="Son" />
</bag>
<bag name="Daughter" table="Daughter" inverse="true" cascade="all-delete-orphan" lazy="false">
<key column="DaughterID" />
<one-to-many class="Daughter" />
</bag>
//on Son/Daughter.hbm.xmlI have
...
<many-to-one name="Bar" column="BarID" class="Bar" lazy="false" />
我在哪里做错了?先感谢您!