NHibernate ManyToMany 关系级联 AllDeleteOrphan StackOverflowException

发布于 2024-09-05 15:32:16 字数 843 浏览 21 评论 0原文

我有两个对象通过映射表相互具有多对多关系。但是,当我尝试保存它时,出现堆栈溢出异常。以下是映射的代码:

//EventMapping.cs
HasManyToMany(x => x.Performers).Table("EventPerformer").Inverse().Cascade.AllDeleteOrphan().LazyLoad().ParentKeyColumn("EventId").ChildKeyColumn("PerformerId");


//PerformerMapping.cs
HasManyToMany<Event>(x => x.Events).Table("EventPerformer").Inverse().Cascade.AllDeleteOrphan().LazyLoad().ParentKeyColumn("PerformerId").ChildKeyColumn("EventId");

当我将 Performermapping.cs 更改为 Cascade.None() 时,我消除了异常,但我的事件对象没有与之关联的执行者。

//In a unit test, paraphrased
event.Performers.Add(performer); //Event
eventRepository.Save<Event>(event);
eventResult = eventRepository.GetById<Event>(event.id); //Event
eventResult.Performers[0]; //is null, should have performer in it

我应该如何正确地写这个? 谢谢

I have two objects that have a ManyToMany relationship with one another through a mapping table. Though, when I try to save it, I get a stack overflow exception. The following is the code for the mappings:

//EventMapping.cs
HasManyToMany(x => x.Performers).Table("EventPerformer").Inverse().Cascade.AllDeleteOrphan().LazyLoad().ParentKeyColumn("EventId").ChildKeyColumn("PerformerId");


//PerformerMapping.cs
HasManyToMany<Event>(x => x.Events).Table("EventPerformer").Inverse().Cascade.AllDeleteOrphan().LazyLoad().ParentKeyColumn("PerformerId").ChildKeyColumn("EventId");

When I change the performermapping.cs to Cascade.None() I get rid of the exception but then my Event Object doesn't have the performer I associate with it.

//In a unit test, paraphrased
event.Performers.Add(performer); //Event
eventRepository.Save<Event>(event);
eventResult = eventRepository.GetById<Event>(event.id); //Event
eventResult.Performers[0]; //is null, should have performer in it

How should I be writing this properly?
Thanks

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

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

发布评论

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

评论(1

岁吢 2024-09-12 15:32:17

您将关系的双方都声明为Inverse,这意味着没有人负责保存关系,当然这是行不通的。

由于您要将事件添加到执行者,因此请从 PerformerMapping 中删除 Inverse 调用。

另外,除非您Flush会话,然后Evict事件,否则使用相同会话调用session.Get将返回相同的对象。

You are declaring both sides of the relationship as Inverse, which means no one is responsible for saving the relationship, and of course will not work.

Since you are adding events to performers, remove the Inverse call from PerformerMapping.

Also, unless you Flush the session and then Evict the event, a call to session.Get using the same session will return the same object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文