0
votes

In nhibernate i am having two tables parent and child...

Parent--> parentid,name

Child --> childid,name,parentid

relation--> one to many

If i insert a record in a parent table at the same time i will insert many records in the child table without any issue..

but if i update the parent record the child table records are not updating instead of that its inserting again..

Below is my mapping code

Parent :

HasMany(x => x.Child)
              .Not.LazyLoad()
              .Cascade.All();

Child : References(x => x.Parent).Not.LazyLoad();

2
show us the code you're using to modify the parent record.Vadim

2 Answers

0
votes

It's very likely the Id generator in childs used. SaveOrUpdate doesnt see them as persisted and tries to insert them. often UnsavedValue("0") can do the trick

Also it would be better to have inverse set to tell NH that the Childs are responsible for the association.

HasMany(x => x.Child)
      .Not.LazyLoad()
      .Inverse()
      .Cascade.All();

// eg: in Parent class
public void Add(Child child)
{
    Childs.Add(child);
    child.Parent = this;
}
0
votes

I found the solution:

Change the parent mapping file with the below code it will delete the old child data and insert new record in the child table

HasMany(x => x.Child)
              .Not.LazyLoad()              
              .Cascade.AllDeleteOrphan();