NHibernate的父映射不创建子外键 [英] NHibernate parent mapping does not create child foreign-key
问题描述
我刚学的NHibernate。我一直在使用的例子从文档和这里的计算器,但我一定是失去了一些东西。
我有一个父对象,有儿童的集合。一个孩子毫无意义没有父母,所以数据库有FK设置为NOT NULL。实施NHibernate的从子到父工作正常,但我没有必要关系这个方向发展。
相反,我试图实现母公司拥有的关系,但我一直得到一个数据库错误冒泡无法插入值NULL到PARENT_ID。无论是ID,也没有父的实体保存时被存储在了孩子。
请参阅下面的code样本。请指教。
类文件
公共类家长{
私人只读的IList<儿童与GT; _children =新的名单,其中,儿童及GT;();
公共虚拟ID {获得;组; }
公共虚拟无效的AddChild(孩童){
_children.add(子);
}
}
公共类儿童{
公共虚拟ID {获得;组; }
}
映射
<类名=父表=父与GT;
<缓存使用率=读写/>
< ID名称=ID列=ID未保存值=0>
<生成器类=身份/>
< / ID>
<包名=儿童访问=field.camelcase下划线级联=全删除,孤儿>
<键列=的ParentId/>
<一到多级=儿童/>
< /袋>
< /类>
<类名=子表=儿童>
<缓存使用率=读写/>
< ID名称=ID列=ID未保存值=0>
<生成器类=身份/>
< / ID>
< /类>
您必须在您的映射和对象模型中定义的关系的双方。然后,声明一个作为逆=真
中的映射。因此,这样的事情应该工作:
.class文件
公共类家长{
私人只读的IList<儿童与GT; _children =新的名单,其中,儿童及GT;();
公共虚拟ID {获得;组; }
公共虚拟无效的AddChild(孩童){
_children.add(子);
}
}
公共类儿童{
公共虚拟家长家长{获得;组; }
公共虚拟ID {获得;组; }
}
映射
<类名=父表=父与GT;
<缓存使用率=读写/>
< ID名称=ID列=ID未保存值=0>
<生成器类=身份/>
< / ID>
<包名=儿童访问=field.camelcase下划线
级联=全删除,孤儿逆=真>
<键列=的ParentId/>
<一到多级=儿童/>
< /袋>
< /类>
<类名=子表=儿童>
<缓存使用率=读写/>
< ID名称=ID列=ID未保存值=0>
<生成器类=身份/>
< / ID>
<多到一个名称=父级=父母栏目=的ParentId/>
< /类>
,你可能希望你的AddChild方法来改变这样的:
公共虚拟无效的AddChild(孩童){
_children.add(子);
child.Parent =这一点;
}
I am just learning NHibernate. I have been using examples from the documentation and here at stackoverflow, but I must be missing something.
I have a PARENT object that has a collection of CHILD. A CHILD is meaningless without a PARENT, so the database has FK set to NOT NULL. Implementing NHibernate from CHILD to PARENT works fine, though I have no need for this direction of relationship.
Instead, I tried to implement the PARENT owning the relationship, but I consistently get a Database error bubbling up "Cannot insert the value NULL into PARENT_ID". Neither the ID nor the entity of the PARENT is being stored in the CHILD when saving.
See the code sample below. Please advise.
Class Files
public class PARENT {
private readonly IList<CHILD> _children = new List<CHILD>();
public virtual Id { get; set; }
public virtual void AddChild(CHILD child) {
_children.add(child);
}
}
public class CHILD {
public virtual Id { get; set; }
}
Mappings
<class name="PARENT" table="Parent">
<cache usage="read-write"/>
<id name="Id" column="Id" unsaved-value="0" >
<generator class="identity" />
</id>
<bag name="Children" access="field.camelcase-underscore" cascade="all-delete-orphan">
<key column="ParentId"/>
<one-to-many class="CHILD"/>
</bag>
</class>
<class name="CHILD" table="Child">
<cache usage="read-write"/>
<id name="Id" column="Id" unsaved-value="0" >
<generator class="identity"/>
</id>
</class>
You must define both sides of the relationship in your mapping and object model. You then declare one as inverse="true"
in the mapping. So something like this should work:
Class files
public class PARENT {
private readonly IList<CHILD> _children = new List<CHILD>();
public virtual Id { get; set; }
public virtual void AddChild(CHILD child) {
_children.add(child);
}
}
public class CHILD {
public virtual PARENT Parent { get; set; }
public virtual Id { get; set; }
}
Mappings
<class name="PARENT" table="Parent">
<cache usage="read-write"/>
<id name="Id" column="Id" unsaved-value="0" >
<generator class="identity" />
</id>
<bag name="Children" access="field.camelcase-underscore"
cascade="all-delete-orphan" inverse="true">
<key column="ParentId"/>
<one-to-many class="CHILD"/>
</bag>
</class>
<class name="CHILD" table="Child">
<cache usage="read-write"/>
<id name="Id" column="Id" unsaved-value="0" >
<generator class="identity"/>
</id>
<many-to-one name="Parent" class="PARENT" column="ParentId" />
</class>
And you might want to change your AddChild method to this:
public virtual void AddChild(CHILD child) {
_children.add(child);
child.Parent = this;
}
这篇关于NHibernate的父映射不创建子外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!