简体   繁体   English

NHibernate级联问题

[英]NHibernate cascading problems

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. 对父级所做的任何更改都将通过更新调用保存到数据库中,但是无论级联值还是inverse = true / false / just-aaveyy,我都无法插入这些子级。 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 我在吵架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: 编辑:读完以下内容: 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 />

with all 6 of the main ones above, with no luck. 以上所有6个主要项目都没有运气。

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? 最初,您的代码中的所有内容看起来都不错,但实际上我想知道您是在哪一侧设置inverse属性?

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. 我希望这有帮助。

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM