1

I have a weird problem at hand. First have a look at my table schema.

A(ID) B(ID,AID) C(ID,AID) D(ID,CID)

The map files are as below:

MAP A
{
HasMany(x => x.B).Cascade.AllDeleteOrphan().Inverse().KeyColumn("AID");
HasMany(x => x.C).Cascade.AllDeleteOrphan().Inverse().KeyColumn("AID");
}

MAP B
{
References(x => x.A).Column("AID");
}

MAP C
{
References(x => x.A).Column("AID");
HasMany(x => x.D).Cascade.AllDeleteOrphan().Inverse().KeyColumn("BID");
}

MAP D
{
References(x => x.C).Column("CID");
}

While doing SaveORUpdate/Merge on A it doesn't insert AID into B and C. But it does insert CID into D. Any suggestions.

Abhishek
  • 11
  • 1

1 Answers1

1

If this happens, you for sure missed to assign both sides of relation. If this would be in place:

var parent = ...;
var child = ...;
parent.Children.Add(child);
child.Parent = parent;

All will work. Because the most supsected here is that your code is like:

var parent = ...;
var child = ...;
parent.Children.Add(child);
// child.Parent = parent; // this is missing

and that won't insert children. Why?

Because we used the .Inverse() mapping. This is a very powerful but fragile setting. It allows NHibernate to do some important optimizations, but that requires - PARENT must be set in child.

Check this nice article

Inverse = “true” example and explanation by mykong

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • I have added relationship properly . Also inverse is set as could be see in example. – Abhishek Jan 30 '15 at 10:32
  • The only thing, which could be seen is the **inverse** in your question. That's why I explained what happens. Because there is no code how you assign child and parent... NHibernate simpley knows: Inverse is in place. I will leave that part on each children. If children are not properly set... nothing would happen. Good news is that the Cascade on Parent is still enough for us to say just session.Save(parent)... *(Just a NOTE also, your question will be more readable for anybody with some more real names, than ABCD)* – Radim Köhler Jan 30 '15 at 10:33
  • in fluent we can just say Inverse() and it works. Also I have done B.A = A C.A = A and D.C = C After this I saved A. – Abhishek Jan 30 '15 at 10:49
  • Oops got the issue. It was automapper issue and not at all related to Nhibernate. Bad bad me. – Abhishek Jan 30 '15 at 11:51
  • ;) Makes sense now ;) NHibernate is might tool ;) Enjoy it sir ;) – Radim Köhler Jan 30 '15 at 12:14