NHibernate 不持久 children objects
NHibernate not persisting children objects
我有两个 object,Parent 和 Child。我一直坚持objectParentparent1。我想创建一个新的 Child object,将其 Parent 属性设置为 "parent1" 并将其添加到 parent1 上的 Child 集合中。
Parent
public class Parent: IEquatable<Parent>
{
public virtual IList<Child> Childs{ get; set; }
...
}
Child
public class Child: IEquatable<Child>
{
public virtual Parent Parent { get; set; }
...
}
Parent.hbm.xml
<bag name="Childs" lazy="false" inverse="true" cascade="all">
<key column="Id" />
<one-to-many class="Child" />
</bag>
Child.hbm.xml
<many-to-one name="Parent">
<column name="Parent" sql-type="int" not-null="true" />
</many-to-one>
运行 这个代码
var child = new Child();
child.Condition = value;
Parent parent= m_parentmanager.Get(2); // "parent1"
parent.Childs.Add(child);
child.Parent = parent;
m_childsmanager.Save(child);
当我再次调用 "Parent parent= m_parentmanager.Get(2)" 下面几行时,我可以在调试模式下看到集合 Childs 是空的。我有一个按钮,可以在我的视图上将 Child 添加到 Parent(到集合 Childs)。当我第一次按下按钮时,此代码运行但 Childs 集合为空。当我第二次按下按钮时,Child 实体被添加到 Childs 集合。所有其他按钮点击也不会将任何新的 Child 实体添加到集合中。
谁能告诉我我做错了什么?
在 DB 中,parent 与 children 的关系以及 children 与 parent 的关系 - 由一个(相同)列定义。它是 child table 中的外键。并且必须在映射的两侧使用
// column must be the one, which is used for many-to-one
<bag name="Childs" lazy="false" inverse="true" cascade="all">
<!--<key column="Id" />-->
<key column="Parent" />
<one-to-many class="Child" />
</bag>
// column Parent represents the relation, it must be used for Childs coll as well
<many-to-one name="Parent">
<column name="Parent" sql-type="int" not-null="true" />
</many-to-one>
此外,集合应该延迟加载。将它们映射为渴望没有任何好处。与其强制映射,不如在查询中急切地加载它。
映射的其余部分似乎没问题 - 颠倒了,两边都在 C# 中赋值...它应该可以工作
我有两个 object,Parent 和 Child。我一直坚持objectParentparent1。我想创建一个新的 Child object,将其 Parent 属性设置为 "parent1" 并将其添加到 parent1 上的 Child 集合中。
Parent
public class Parent: IEquatable<Parent>
{
public virtual IList<Child> Childs{ get; set; }
...
}
Child
public class Child: IEquatable<Child>
{
public virtual Parent Parent { get; set; }
...
}
Parent.hbm.xml
<bag name="Childs" lazy="false" inverse="true" cascade="all">
<key column="Id" />
<one-to-many class="Child" />
</bag>
Child.hbm.xml
<many-to-one name="Parent">
<column name="Parent" sql-type="int" not-null="true" />
</many-to-one>
运行 这个代码
var child = new Child();
child.Condition = value;
Parent parent= m_parentmanager.Get(2); // "parent1"
parent.Childs.Add(child);
child.Parent = parent;
m_childsmanager.Save(child);
当我再次调用 "Parent parent= m_parentmanager.Get(2)" 下面几行时,我可以在调试模式下看到集合 Childs 是空的。我有一个按钮,可以在我的视图上将 Child 添加到 Parent(到集合 Childs)。当我第一次按下按钮时,此代码运行但 Childs 集合为空。当我第二次按下按钮时,Child 实体被添加到 Childs 集合。所有其他按钮点击也不会将任何新的 Child 实体添加到集合中。
谁能告诉我我做错了什么?
在 DB 中,parent 与 children 的关系以及 children 与 parent 的关系 - 由一个(相同)列定义。它是 child table 中的外键。并且必须在映射的两侧使用
// column must be the one, which is used for many-to-one
<bag name="Childs" lazy="false" inverse="true" cascade="all">
<!--<key column="Id" />-->
<key column="Parent" />
<one-to-many class="Child" />
</bag>
// column Parent represents the relation, it must be used for Childs coll as well
<many-to-one name="Parent">
<column name="Parent" sql-type="int" not-null="true" />
</many-to-one>
此外,集合应该延迟加载。将它们映射为渴望没有任何好处。与其强制映射,不如在查询中急切地加载它。
映射的其余部分似乎没问题 - 颠倒了,两边都在 C# 中赋值...它应该可以工作