Correct order to create a record with children?

Topics: Core, General, Troubleshooting, Writing modules
Oct 31, 2012 at 7:29 PM
Edited Oct 31, 2012 at 7:29 PM

So let's say that we have 2 record classes A and B

A has a property 'Children' that is a list of B instances (and B has a property A_Id to reference the parent)

Now 2 scenarios:

1) If I create a new A record and want to add 3 new B instances to the children list. In what order do I assign the parent / add the child and 'SaveOrUpdate' them?

2) I have an existing A record and I want to add 3 new B instances to the children list. In what order do I assign the parent / add the child and 'SaveOrUpdate' them?

If it matters : We're on 1.6 now.

Developer
Oct 31, 2012 at 7:39 PM

I usually just add to the collection and let NHibernate figure it out. If you need to access the ParentId during the same request, be sure to assign it. I never had to worry about the order, but maybe I was lucky.

Oct 31, 2012 at 7:42 PM

But do I need to set both the B 'parent' record + add it to the A children list? And do I SaveOrUpdate just the A record or all B AND A records?

Thing is that I sometimes get the following exception in one of my migration commands that I'm working on: [StaleStateException: Batch update returned unexpected row count from update; actual row count: 0; expected: 1]

Developer
Oct 31, 2012 at 7:45 PM

I don't think you have to set the parent record, but you do need to make sure the child records are saved first before you save the parent record. That said, it shouldn't hurt to also set the parent record as well as adding the children to this parent.