开发者

NHibernate ManyToMany Relationship Cascading AllDeleteOrphan StackOverflowException

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 ex开发者_开发知识库ception. 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜