NHibernate级联问题
我在一个类中具有此映射:
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对我来说,一次性正确设置它总是一个问题。起初,您的代码中的所有内容看起来都不错,但我实际上想知道您在哪一侧设置逆属性?
这段代码对我有用:
我希望这有帮助。
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:
I hope this helps.
这是正确的映射:
以及正确的用法(假设已经存在父级):
This is the correct mapping:
And the correct usage (assumes an already existing parent):