This issue occurred when
- There are two entities, same class, same table, different entity names.
- Both entities have one-to-many relationship to a child table
- Both one-to-many relationships specify the foreign key column as not-null=”true”
- Try to call Session.Merge on one entity, with additional new child entities inside.
After trying random configurations, somehow removing not-null=”true” from one entity hbm mapping solves this issue.
No idea why, the only way is, perhaps, to debug NHibernate code.
Update in Jan 2021: Seems that setting inverse = “true” to one of the entities will solve the problem
For inverse = “true” in NHibernate, refer to this link: https://mkyong.com/hibernate/inverse-true-example-and-explanation/
It’s also possible to fix this issue by setting up both one-to-many and many-to-one relationships between 2 entities.