Replicate - Related entity persisted to DB but NOT attached to ObjectManager?


Please consider the following code:

    LSchematic := SchematicFromNode(LNodeSchematic);  // LSchematic is a TSchematic entity
    LSettings := SettingsFromNode(LNodeSettings);  // LSettings is a TSettings entity
      LSchematic.Settings := LSettings;  // Set the "association field"
      LSchematicResult := FObjMgr.Replicate(LSchematic); // Replicate. Schematic entity (ID) may already exist
      FObjMgr.Flush; // Ensure all written to database
      SchematicID := LSchematicResult.ID; // Return ID. Used to update UI.
      if not FObjMgr.IsAttached(LSettings) then
        LSettings.Free; // *** THIS CODE IS ALWAYS EXECUTED

When I Replicate the TSchematic - together with the TSettings entity (!!) - and then Flush the ObjectManager, both entities are correctly persisted to the database. Yet the TSettings entity is not attached to the ObjectManager.

This doesn't make sense to me; I would expect that - if the TSettings is persisted together with the TSchematic (by association) - both entities would be attached to the ObjectManager after the Flush operation.

What am I missing? Does Replicate also spawn a new TSettings entity (by association) which is attached to the ObjectManager?

Rgds, Mark

Exactly. Replicate (and Merge) receives an entity that is not in the manager, and always load (or create) and return an entity that is in the manager, which is different from the one you passed.
If Settings mapping has the TCascadeType.Merge in its cascade configuration, then the replication operation cascades to Settings, which performs exactly the same way.

This the whole object tree you pass to Replicate/Merge will return a whole "cloned" object tree that is supposed to be in the manager.