Nhibernate 3.3. one-to-many code updates child elements instead of pasting

I have this one-to-many association that I created zilions of times with the old nhibernate or freely. But I can’t get it to work with cartographic code.

These are classes

 public class Parent
 {
      public virtual IList<Child> Children { get; set; }
 }

 public class Child
 {
      public virtual Parent Parent { get; set; }
 }

Nothing odd

and these are mapping classes

For parent:

 Bag(x => x.Parent, m => m.Key(k => k.Column("Parent_id")));

Child:

 ManyToOne(x => x.Children, map => { map.Column("Parent_id"); map.Cascade(Cascade.All); });

If I do the following

 var parent = new Parent();
 parent.Children.Add(new Child());

 session.SaveOrUpdate(parent); 

I got the correct INSERT for the parent, but it does UPDATE for any child added

UPDATE TableChildren
......
WHERE Id = 0 <-????

What am I missing? I hit my head!

+3
source share
1 answer

I see two questions. It seems that the mapping is inverted ( Bagshould go for Children, ManyToOnefor a Parent). The setting is important here inverse="true".

As described in detail here:

:

Bag(x => x.Children, m => 
    m.Inverse(true);
    m.Cascade(Cascade.All);
    m.Key(k => k.Column("Parent_id")));

ManyToOne(x => x.Parent, map => 
{ 
    map.Column("Parent_id"); 
});

inverse="true" - NHibernate, . , Children, Parent! NHibernate .

+6

All Articles