StrataFrame Forum

Save child/BusinessLayerLinkManager also save ParentBO

http://forum.strataframe.net/Topic7156.aspx

By Chan - 2/23/2007

Hi,

I have a maintenance form that allow user to enter Purchase Invoice data. In same screen, I also allow user to do "immediate" stock transfer.

So, I have tables as below

PurchaseInvoice ---------<- PInvoiceTransfer ->------------- Transfers ---------<- TransferDetails
                          1:1          (n:n link table)            1:1                          1:n

Currently, I save them using transaction with following sequence

Form.Save() ------------> PurchaseInvoice
Form.AfterSave() ------------> Transfer.Save() 
                        ------------> TransferDetails.Save()
                        ------------> PinvoiceTransfer.Save()

The problem is, when I call TransferDetails.Save(), it also call Transfer.Save() since Transfer is ParentBO of TransferDetails and it is still dirty. Therefore, Transfer record will be inserted to tabel twice and cause uniqueness record error.

I tried not to call Transfer.Save(), only call TransferDetails.Save(). It works fine. This time, PInvoiceTransfer cause same problem. PInvoiceTransfer.Save() try to call Transfer.Save() also.

How could I solve this problem?

Thank you

By StrataFrame Team - 2/26/2007

Well, when you save business objects on a transaction, they are left "dirty" until you call TransactionCommit(), at which point, all of the changes to them are accepted.  Also, when you attempt to save a child business object, it will make sure that it's parent is not dirty before saving... if it is dirty, then it saves it's parent first (so that there aren't any negative FKs that need to be assigned by the server, first).

So, you can either call PinvoiceTransfer.Save(), which will cascade the saves up the parent chain, or wrap the .Save() calls with .ParentBusinessObject = Nothing and .ParentBusinessObject = parent.  If a business object doesn't have a parent, it won't try to save the parent.  So, you set the parent to nothing and then set the parent back after the save.