同时插入NHibernate的父母和孩子 [英] Inserting parent and children with NHibernate at the same time

查看:123
本文介绍了同时插入NHibernate的父母和孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图保存(插入)一个Parent实体和列表Child实体。这两个实体都使用Guid作为主键。这些键是不可空的,并没有在数据库中建立关键的关系。

保存不起作用,引发异常 - 我正试图将Child保存到Child表中的父外键。



我期待nhibernate为Parent创建一个键,并让它的Child对象知道它。这是NHibernate的限制或使用Guids作为主键?



这是 answer 建议你必须设置父级manully,这真的是唯一的方法吗?
$ b $ p

这是我的映射

父映射:

  HasMany(x => ; x.Children).KeyColumn(ParentKey)。Inverse()。Cascade.All(); 

子映射:

 引用(x => x.Parent).Not.Nullable()。Column(ParentKey); 


解决方案

这只是一个ORM,如果你的孩子没有对父母的引用,为什么会假设因为父母有一个孩子的名单,那么这个孩子的引用又应该是对父母的引用? / p>

我相信你在回答自己的问题的时候说过Child实体没有填充Parent属性(这意味着它是null,这意味着NHibernate会试图在你的Child表中为Parent id保存一个'null'值)。

如果你想在不使用NHibernate的情况下使用这些对象,那么Parent引用需要当它们被添加时,在子实体上被设置。

编辑:这是为了指定'反向' 在你的地图上。如果您要将此调用移除到'反向',,它应该按照您希望它以反向方式工作的方式工作,即另一端 (儿童实体)负责跟踪这种关系。这意味着您需要手动设置子对象的引用。



但是,删除反向语句会导致Child )被保存,父节点被保存,然后Child(ren)的父节点ID被更新。由于您对父ID有空限制,这意味着它仍然不能正常工作,因为它最初会插入带有父ID为空的子。



解决方案是去除这个约束,或者只是添加一个名为 AddChild:

的方法

  public void AddChild(Child childObj)
{
childObj.Parent = this;
Children.Add(childObj);
}

添加另一个名为 RemoveChild:

  public void RemoveChild(Child childObj)
{
if(Children.Contains(childObj))
{
Child.Parent = null;
Children.Remove(childObj);






$ b然后,使用这些方法来添加/移除子元素。


I am attempting to save(insert) a Parent entity with list Child entities. Both entites use a Guid as primary keys. The keys are none nullable and there is no key relationship set up in the db.

The save doesn't work, an exception is thrown claiming - that I am trying to save a null into Parent foreign key in the Child table.

I was expecting nhibernate to create a key for the Parent and let its Child objects know about it. Is this a limitation of NHibernate or of using Guids as primary keys?

This answer suggests you have to set the parent manully, is this really the only way?

Here are my mappings for reference:

Parent mapping:

HasMany(x => x.Children).KeyColumn("ParentKey").Inverse().Cascade.All();

Child mapping:

 References(x => x.Parent).Not.Nullable().Column("ParentKey");

解决方案

NHibernate is not magic. It is only an ORM, if your children do not have their reference set to the parent, why would it assume that because the parent has a list of children that the childrens' reference, in turn, SHOULD be a reference to the parent?

I believe you answered your own question when you stated that the Child entities don't have their Parent property populated (which means it is null, which means NHibernate would be attempting to save a 'null' value for the Parent id in your Child table).

If you were to use these objects WITHOUT NHibernate it would make sense that the Parent reference needs to be set on the Child entities when they are added.

EDIT: This is for the case where you have specified 'Inverse' on your mapping. If you were to remove this call to 'Inverse,' it should work the way you wanted it to work as Inverse states that the other end (the child entity) is responsible for keeping track of the relationship. This means you need to set the reference of the parent on the child manually.

However, removing the Inverse statement would cause the Child(ren) to be saved, the parent to be saved, then the Child(ren)'s parent id to be UPDATED. Since you have a null constraint on the parent id, this means it still won't work as it would initially insert the Child with a parent id of null.

The two solutions would be to either remove this constraint, or to just add a method to the Parent called AddChild:

public void AddChild(Child childObj)
{
    childObj.Parent = this;
    Children.Add(childObj);
}

Add another method called RemoveChild:

public void RemoveChild(Child childObj)
{
    if (Children.Contains(childObj))
    {
        Child.Parent = null;
        Children.Remove(childObj);
    }
}

Then, just use those methods for adding/removing children.

这篇关于同时插入NHibernate的父母和孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆