NHibernate ManyToMany 关系级联 AllDeleteOrphan StackOverflowException
我有两个对象通过映射表相互具有多对多关系。但是,当我尝试保存它时,出现堆栈溢出异常。以下是映射的代码:
//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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将关系的双方都声明为
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 thenEvict
the event, a call tosession.Get
using the same session will return the same object.