如何在 nhibernate 中保存指定 id 的孩子 [英] How to save a child with assigned id in nhibernate

查看:18
本文介绍了如何在 nhibernate 中保存指定 id 的孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个班级:

public class Parent
{
    public virtual long? ID { get; set; } // native
    public virtual IList<Child> Children { get; set; }
    public virtual string Name { get; set; }
}

public class Child
{
    public virtual long ID { get; set; } // assigned
    public virtual string Name { get; set; }
}

实例化和保存父子节点:

Instantiating and saving parent and child:

child = new Child() { ID = 1, Name = "SomeName" };
parent = new Parent() { Children = new List() { child } };
session.Save(parent);

这给了我:

NHibernate.StaleStateException:意外的行数:0;预期:1.

我认为问题出在孩子身上分配的 id 上.因为它有一个id,所以NHibernate认为它之前已经保存过,但事实并非如此.

I think the problem is with the assigned id on the child. Since it has an id, NHibernate thinks it has previously saved before which is not the case.

生成的(修剪和重命名的)SQL是:

The generated (trimmed & renamed) SQL is:

NHibernate: select child0_.ID as child1_1_, child0_.NAME as NAME1_, child0_.PARENT_ID as COMMAND7_1_, from CHILD child0_
NHibernate: select parent0_.PARENT_ID as parent1_10_
NHibernate: select parent0_.PARENT_ID as parent1_10_, parent0_.NAME as parent2_10_ from PARENT parent0_
NHibernate: UPDATE CHILD SET PARENT_ID = @p0 WHERE CHILD_ID = @p1;@p0 = 2, @p1 = 1

映射文件:

<class name="MyNamespace.Child" table="CHILD">
  <id name="ID" column="CHILD_ID" type="System.Int64">
    <generator class="assigned"></generator>
  </id>
  <property name="Name" column="NAME"></property>
</class>

<class name="MyNamespace.Parent" table="PARENT">
  <id name="ID" column="PARENT_ID" type="System.Int64">
    <generator class="native"></generator>
  </id>
  <property name="Name" column="NAME"></property>
  <bag name="Children">
    <key column="PARENT_ID"></key>
    <one-to-many class="MyNamespace.Child"></one-to-many>
  </bag>
</class>

在搜索谷歌时,我发现了关于版本标签这可能是一个解决方案,但我没有一个持久性字段用作版本.在这种情况下,如何保存(插入)具有指定 ID 及其父级的子级?

While searching google, I found about version tag which may be a solution but I do not have a persistent field to use as version. In this case, how can I save (insert) a child with assigned id and its parent?

推荐答案

当从父级级联到子级时,NHibernate 使用 SaveOrUpdate 方法.您是正确的,NHibernate 需要某种方式来确定它是否应该执行插入或更新.它将查看三个不同字段的未保存值以确定实体是否是新的.

When cascading from a parent to a child, NHibernate uses the SaveOrUpdate method. You are correct that NHibernate need some way to determine whether it should perform an insert or an update. It will look at three different fields for an unsaved value to determine if the entity is new.

  1. 身份证
  2. 版本
  3. 时间戳

对于分配的 Id,您将需要 Version 或 Timestamp 字段以指示实体是新的.

With an assigned Id, you will need either a Version or Timestamp field in order to indicate that the entity is new.

另一种方法是对子项显式调用 Save().

An alternative would be to call Save() on the children explicitly.

这篇关于如何在 nhibernate 中保存指定 id 的孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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