NHibernate:级联保存到孩子不插入 [英] NHibernate: Cascading saves to children does not insert

查看:102
本文介绍了NHibernate:级联保存到孩子不插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的双向关联:

  public class Parent 
{
public int ParentId {得到; set;}
...其他属性
public IEnumerable< Child>孩子{get; set;}
}

public class Child
{
public int ChildId {get; set;}
...其他属性
public Parent Parent {get; set;}
}

流畅的映射如下:



父映射

  Id(x => x。 ParentId,PARENT_ID)。GeneratedBy.Identity(); 
..其他映射
HasMany(x => x.Children).Cascade.All()。KeyColumn(PARENT_ID);

子图映射

  Id(x => x.ChildId,CHILD_ID)。GeneratedBy.Identity(); 
..其他映射
引用(x => x.Parent).Column(PARENT_ID)。Cascade.None();

当我执行这样的代码时:

 父亲=新父亲{..设置一些属性...}; 
parent.Children = new [] {new Child {..set some properties ..};
session.Save(parent);

由于NHibernate没有设置 PARENT_ID

显然,我已经要求在<$ c的映射中级联$ C>父。 NHibernate试图保存孩子,但为什么没有设置ID?

解决方案

您需要进行两项更改。 / p>


  1. HasMany()上需要 .Inverse x => x.Children)。有关更多信息,请参阅我的答案解释
  2. 您还需要添加 child.Parent = parent; 添加到保存实体的代码中。

你的情况,这两个关系是相互冲突的。 parent.Children 包含 child ,这意味着NHibernate应该坚持 Child.PARENT_ID 作为父母的Id,但是 child.Parent 为null,这意味着NHibernate应该坚持 Child.PARENT_ID 为空。显然 child.Parent 赢了。其实更可能的是他们都赢了。 NHibernate可能执行了两个类似于这些的查询...

$ p $ / *这就是我们应该保存的child.Parent。
此查询将失败,因为PARENT_ID不是NULL。 * /
插入到Child(CHILD_ID,PARENT_ID)值(@childId,null);

/ *这就是parent.Children说的我们应该保存的东西。 * /
更新子集PARENT_ID = @parentId其中CHILD_ID = @childId;

如果你做了两个我上面推荐的更改,NHibernate将能够正确保存,就像这样:插入到Child(CHILD_ID,PARENT_ID)的值(@childId,@parentId);

  


I have a bidirectional association like this:

public class Parent
{
  public int ParentId {get; set;}
  ...other properties
  public IEnumerable<Child> Children {get; set;}
}

public class Child
{
  public int ChildId {get; set;}
  ...other properties
  public Parent Parent {get; set;}
}

The fluent mappings are as follows:

Parent mapping

Id(x => x.ParentId, "PARENT_ID").GeneratedBy.Identity();
.. other mappings
HasMany(x => x.Children).Cascade.All().KeyColumn("PARENT_ID");

Child mapping

Id(x => x.ChildId, "CHILD_ID").GeneratedBy.Identity();
.. other mappings
References(x => x.Parent).Column("PARENT_ID").Cascade.None();

When I execute code like this:

Parent parent = new Parent{ ..set some properties... };
parent.Children = new[] { new Child{ ..set some properties.. };
session.Save(parent);

I get a foreign key constraint violation because NHibernate is not setting the PARENT_ID column of the child record to the new ID when it attempts to insert the child.

Clearly I have requested cascading in the mapping for Parent. NHibernate is trying to save the child, but why is the ID not being set?

解决方案

You need to make two changes.

  1. You need .Inverse() on the HasMany(x => x.Children). See my answer explaining inverse for more information.
  2. You also need to add child.Parent = parent; to the code that saves the entities.

In your case, the two relationships are conflicting with one another. parent.Children contains child, which means that NHibernate should persist Child.PARENT_ID as the parent's Id, but child.Parent is null, meaning that NHibernate should persist Child.PARENT_ID as null. Apparently child.Parent won. Actually, what's more likely is that they both won. NHibernate was probably executing two queries similar to these...

/* This is what child.Parent says we should save.
   This query will fail because PARENT_ID is NOT NULL. */
insert into Child (CHILD_ID, PARENT_ID) values (@childId, null);

/* This is what parent.Children says we should save. */
update Child set PARENT_ID = @parentId where CHILD_ID = @childId;

If you make the two changes I recommend above, NHibernate will be able to save this correctly, like so:

insert into Child (CHILD_ID, PARENT_ID) values (@childId, @parentId);

这篇关于NHibernate:级联保存到孩子不插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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