How to post a new entity containing proxied subenties

Lets say that I have entity like (unimportat removed)

  [ Entity, Automapping ]
  [ Id( 'fid', TIdGenerator.IdentityOrSequence ) ]
  [ table( ''purorders' ) ]
  Tpurchaseorder = class
    [ Column( 'tilausnro', [ ] ) ]
    Fid: Integer;
     [ Association( [ TAssociationProp.Lazy ], [ ] ) ]
    [ JoinColumn( 'toimittaja', [ ] ) ]

   id: integer ;
      Read Fid; 
    property supplier: TSupplier;
      Read GetSupplier
      Write SetSupplier;

I try to post
a supplier with id 0004 exists in db.
i get error AssociationReferencesTransientObject
Association references a transient object. \r\nParent: purchaseorder.entities.Tpurchaseorder(12157), Transient: supplier.entities.TSupplier(0004), Association: Fsupplier

What I'm missing...

Had to get up from bed to test this idea. And it worked.
So documenting it here for others / my team/and to dementic myself in future.
mark FSupplier with [transient]
Create addtional property Supplier:string read getSupplier write setsupplier; and mark that property with
[xdataProperty] .
In setsupplier just find correct supplier and assing it to FSupplier.value. Object manager can be found
So only problem is what happens if some coder writes to supplier property to write without xdata ......

You shouldn't need to do all that. The problem you are getting is because you are missing the proper cascades in your association. I assume it was a mistake. Unless of course you have a good reason to not have any cascade in your mapping, in this case your solution is fine.
But if it's not case, you should just add TCascadeType.SaveUpdate and TCascadeType.Merge to the association. Or, even better, add all but remove, which is the recommended cascade for single associations:

    [ Association( [ TAssociationProp.Lazy ], CascadeTypeAllButRemove ) ]
    [ JoinColumn( 'toimittaja', [ ] ) ]

Posting a new order with
..., "supplier" :{"id":"0004"} with CascadeTypeAllButRemove emptied all other fields of supplier :rofl:
changed to Merge worked for me saved all existing data.
I assume that his is excepted behaviour

Using extra property with [xdataProperty] attribute makes it possible to present id in simplier format
"supplier": "0004".

Yes, if your supplier has empty fields. Or you can remove TCascadeType.Flush from cascade, then the supplier fields won't be updated.

Yes, if that's what you want, you can use it.

1 Like

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.