Thanks alot! I remember this now from Evans DDD book and it makes perfect sense.
On Sat, Aug 28, 2010 at 2:11 PM, nadav s <[email protected]> wrote: > when inverse is set to true, and you add the element to the entity, you > gotta set its many-to-one relationship property, meanning, you gotta have a > method called AddElement(Element elem) > that does this: > > this.Elements.Add(elem); > elem.Entity = this // this is the back reference > > didn't get into the test2 and test3, but using the ICollection Elemenets to > add elements from outside the Entity class is usually a bad idea. > > > On Sat, Aug 28, 2010 at 2:57 PM, Jacob Madsen <[email protected]> wrote: > >> Hi there, >> >> I have written 3 tests to show my issue. >> >> Test 1 is failing and I would like to hear if its possible to get this >> working like in Test 3. Test 2 is also failing, but this is expected. >> >> I have the following object model. "Entity" is an aggregate root so >> "Element" will not be updated/saved/deleted directly (I know according to >> aggregate root pattern that its wrong invoke entity.Elements.Clear() >> directly as I do below - its an object model with the purpose of simplicity >> in this scenario). >> >> public class Entity >> { >> public int Id { get; set; } >> >> IList<Element> Elements { get; set; } >> >> public Entity() >> { >> Elements = new List<Element>(); >> } >> } >> >> public class Element >> { >> public int Id { get; set; } >> } >> >> Test 1: >> >> ------------------------------------------------------------------------------------------------------------------- >> The following test is performed when cascade="all-delete-orphan" >> inverse="true" is set for the bag. >> >> int entityId = 0; >> >> using (var session = sessionFactory.OpenSession()) >> using (var tx = session.BeingTransaction()) >> { >> var entity = new Entity(); >> entity.Elements.Add(new Element()); >> >> entityId = (int)session.Save(entity); >> >> tx.Commit(); >> } >> >> using (var session = sessionFactory.OpenSession()) >> using (var tx = session.BeingTransaction()) >> { >> var entity = session.Get<Entity>(entityId); >> >> Assert.That(entity.Elements.Count, Is.EqualTo(1)); // Fail, the count >> is equal to 0!!! I can see in the Element row data that the FK is NULL. >> } >> >> Test 2: >> >> ------------------------------------------------------------------------------------------------------------------- >> The following test is performed when cascade="all-delete-orphan" >> inverse="false" is set for the bag. >> >> int entityId = 0; >> >> using (var session = sessionFactory.OpenSession()) >> using (var tx = session.BeingTransaction()) >> { >> var entity = new Entity(); >> entity.Elements.Add(new Element()); >> >> entityId = (int)session.Save(entity); >> >> tx.Commit(); >> } >> >> using (var session = sessionFactory.OpenSession()) >> using (var tx = session.BeingTransaction()) >> { >> var entity = session.Get<Entity>(entityId); >> >> Assert.That(entity.Elements.Count, Is.EqualTo(1)); // Success >> >> entity.Elements.Clear(); >> >> tx.Commit(); // >> Exception: NHibernate.ObjectDeletedException : deleted object would be >> re-saved by cascade (remove deleted object from associations) >> } >> >> Test 3: >> >> ------------------------------------------------------------------------------------------------------------------- >> The following test is performed when cascade="all-delete-orphan" >> inverse="true" is set for the bag. >> >> int entityId = 0; >> >> using (var session = sessionFactory.OpenSession()) >> using (var tx = session.BeingTransaction()) >> { >> var entity = new Entity(); >> >> entityId = (int)session.Save(entity); >> >> tx.Commit(); >> } >> >> using (var session = sessionFactory.OpenSession()) >> using (var tx = session.BeingTransaction()) >> { >> var entity = session.Get<Entity>(entityId); >> >> entity.Elements.Add(new Element()); >> >> session.Update(entity); >> >> tx.Commit(); >> } >> >> using (var session = sessionFactory.OpenSession()) >> using (var tx = session.BeingTransaction()) >> { >> var entity = session.Get<Entity>(entityId); >> >> Assert.That(entity.Elements.Count, Is.EqualTo(1)); // Success >> } >> >> -- >> You received this message because you are subscribed to the Google Groups >> "nhusers" group. >> To post to this group, send email to [email protected]. >> To unsubscribe from this group, send email to >> [email protected]<nhusers%[email protected]> >> . >> For more options, visit this group at >> http://groups.google.com/group/nhusers?hl=en. >> > > -- > You received this message because you are subscribed to the Google Groups > "nhusers" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]<nhusers%[email protected]> > . > For more options, visit this group at > http://groups.google.com/group/nhusers?hl=en. > -- You received this message because you are subscribed to the Google Groups "nhusers" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.
