NHibernate级联问题

发布于 2024-10-26 16:58:49 字数 1810 浏览 5 评论 0原文

我在一个类中具有此映射:

<class name="Parent">
<set name="Activity" table="ChangeLogs" order-by="ChangeDate desc" cascade="all-delete-orphan">
  <key column="RequestID" />
  <one-to-many class="ChangeLog" />
</set>

在另一个类中具有此映射:

<class Name="Child">
<many-to-one name="Request" class="Request" column="RequestID" />

在父级中,加载到当前事务中,我添加到集合中:

parent.Activity.Add(new Child(){/* properties, etc */});

然后提交事务。对父级的任何更改都会通过更新调用保存到数据库中,但无论级联值或 inverse=true/false/just-save-already 如何,我都无法让这些子级插入。我已经用我的头敲打它有一段时间了,阅读示例/文档/等,但我不明白为什么它不起作用。我错过了一些简单的事情吗?我一直在关闭服务器并在每次更改后重新构建以确保内容正在更新,但什么也没有。

更糟糕的是,有时它可以工作,具有各种相反的值,有时通过将 Request=parent 添加到子项,有时通过在添加之前单独保存子项...然后它停止工作稍后。我令人难以置信的 o_O

编辑:自从发布此内容以来,已经明确尝试过,在每个之间重建+服务器重新启动,其中没有一个生成插入调用或错误:

<set inverse=true>
parent.Activity.Add(new Child(){Request=parent})
<set inverse=true>
parent.Activity.Add(new Child(){})
<set>
parent.Activity.Add(new Child(){Request=parent})
<set>
parent.Activity.Add(new Child(){})
<set inverse=false>
parent.Activity.Add(new Child(){Request=parent})
<set inverse=false>
parent.Activity.Add(new Child(){})

这确实有效:

<set>
Child c = new Child() {Request = parent};
parent.Activity.Add(c);
Session.Save(c);

但是如果忽略它,那么设置级联有什么意义呢?

编辑:阅读了一些内容后: http://nhibernate. info/doc/nh/en/index.html#example-parentchild-bidir 我尝试过:

<class Name="Child">
<many-to-one name="Request" class="Request" column="RequestID" not-null=true />

上面所有 6 个主要的,但没有运气。

I have this mapping in one class:

<class name="Parent">
<set name="Activity" table="ChangeLogs" order-by="ChangeDate desc" cascade="all-delete-orphan">
  <key column="RequestID" />
  <one-to-many class="ChangeLog" />
</set>

And this in the other:

<class Name="Child">
<many-to-one name="Request" class="Request" column="RequestID" />

In the parent, loaded in the current transaction I add to the collection:

parent.Activity.Add(new Child(){/* properties, etc */});

And then I commit the transaction. Any changes to the parent get saved to the database with an update call, but I can't get those children to insert, regardless of cascade values or inverse=true/false/just-save-already. I've been bashing my head against it for a while, reading examples / documentation / etc, and I can't see why it wouldn't work. Am I missing something simple? I've been killing off the server and re-building after every change to make sure things are updating, but nada.

To make matters worse, occasionally it has worked, with various values for inverse, sometimes by adding the Request=parent to the child, sometimes by saving the child separately before adding... and then it quits working later on. I'm boggling o_O

edit: Things tried explicitly since this was posted, rebuild + server restart between every one, none of which generate an insert call or errors:

<set inverse=true>
parent.Activity.Add(new Child(){Request=parent})
<set inverse=true>
parent.Activity.Add(new Child(){})
<set>
parent.Activity.Add(new Child(){Request=parent})
<set>
parent.Activity.Add(new Child(){})
<set inverse=false>
parent.Activity.Add(new Child(){Request=parent})
<set inverse=false>
parent.Activity.Add(new Child(){})

This did work:

<set>
Child c = new Child() {Request = parent};
parent.Activity.Add(c);
Session.Save(c);

But then what's the point of setting a cascade if it's ignored?

edit: after reading a bit of this: http://nhibernate.info/doc/nh/en/index.html#example-parentchild-bidir I tried:

<class Name="Child">
<many-to-one name="Request" class="Request" column="RequestID" not-null=true />

with all 6 of the main ones above, with no luck.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风筝在阴天搁浅。 2024-11-02 16:58:49

对我来说,一次性正确设置它总是一个问题。起初,您的代码中的所有内容看起来都不错,但我实际上想知道您在哪一侧设置逆属性?

这段代码对我有用:

<class name="Parent" table="Parent">

<bag name="Languages" inverse="true" cascade="all-delete-orphan" lazy="false" fetch="join">
  <key column="parentId" />
  <one-to-many class="Language" />
</bag>

<class name="Language" table="Language">

<many-to-one column="parentId" name="Parent" class="Parent" not-null="true" />

我希望这有帮助。

For me it's always a problem to set it properly at one go. At first everything looks good in your code, but I'm actually wondering on which side you are setting the inverse property?

This code works for me:

<class name="Parent" table="Parent">

<bag name="Languages" inverse="true" cascade="all-delete-orphan" lazy="false" fetch="join">
  <key column="parentId" />
  <one-to-many class="Language" />
</bag>

<class name="Language" table="Language">

<many-to-one column="parentId" name="Parent" class="Parent" not-null="true" />

I hope this helps.

柠栀 2024-11-02 16:58:49

这是正确的映射:

<class name="Parent">
  ...
  <set cascade="all-delete-orphan" inverse="true" ...>
  ...
  </set>
</class>
<class name="Child">
  ...
  <many-to-one .../>
</class>

以及正确的用法(假设已经存在父级):

using (var tx = session.BeginTransaction())
{
    var parent = GetParent();
    parent.Collection.Add(new Child { Parent = parent });
    tx.Commit();
}

This is the correct mapping:

<class name="Parent">
  ...
  <set cascade="all-delete-orphan" inverse="true" ...>
  ...
  </set>
</class>
<class name="Child">
  ...
  <many-to-one .../>
</class>

And the correct usage (assumes an already existing parent):

using (var tx = session.BeginTransaction())
{
    var parent = GetParent();
    parent.Collection.Add(new Child { Parent = parent });
    tx.Commit();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文