Deep Persistence

Deep persistence refers to the ability to simultaneously persist two related items in SAP Commerce Cloud. This topic describes the available deep persistence methods and modifications to the EDMX schema in support of deep persistence.

The deep persistence behavior in the type system uses the partOf attribute to create a dependent relationship and to enforce deep persistence on certain items. For example, an Order Entry cannot exist in the system without an Order. Therefore, when you create an Order Entry item, the system also persists a related Order item, using the partOf attribute to indicate the dependent relationship.

To deep-persist an item that does not exist in the type system, or to deep-persist an existing item for which the partOf attribute is false, you can do one of the following:
  • For new or existing items that do not have partOf=true in the type system, set the autoCreate flag to true. For example, you might need to create a Unit attribute while persisting the Product object as part of the same Product Integration Object.

  • Expert users only: For existing items, update items.xml with partOf=true. Restart SAP Commerce, rebuilding and redeploying the extensions.

You can see whether partOf=true for an Integration Object Item in the Backoffice Integration UI Tool. For more information, see the Viewing Details section in Modeling Features.

Differences between partOf and autoCreate

The partOf attribute is a type system feature, while autoCreate is a feature of the Integration API module. The partOf attribute, when set to true, specifies a relationship and also creates a dependency between two items. For example, because the Order Entry item cannot exist in the system without an Order item, deleting an Order item also deletes the related Order Entry items.

The autoCreate annotation, however, specifies a relationship and enforces deep persistence, but does not create a dependency between two items. For example, if you use autoCreate to set up a deep persistence relationship between Product and Catalog, and later delete a Product item, the API does not delete the associated Catalog item.

For more information about partOf behavior, see Relations.

Deep persistence using autoCreate

Set the autoCreate flag to true in the IntegrationObjectItemAttribute definition at the attribute level to deep-persist a nested integration object. You can make this change using the autoCreate checkbox in the Backoffice Integration UI Tool, or by importing an ImpEx script.

You can view a sample ImpEx script and EDMX schema in the Deep persistence examples section of this topic.

For information about modeling Integration Objects in Backoffice, see Modeling Integration Object Metadata.

Supporting EDMX schema modifications

While the partOf flag in the type system indicates whether the assigned attribute value belongs to the current instance of the specified type, the partOf flag does not appear in the EDMX schema generated for the Integration API. Therefore, the EDMX schema’s NavigationProperty contains the engineered annotations, IsPartOf and IsAutoCreate, to indicate whether the related type is deep-persisted.

In the EDMX schema:
  • IsPartOf appears when partOf=true in the type system or in items.xml.

  • IsAutoCreate appears when you set autoCreate to true as described in this topic.

If both the partOf and autoCreate flags are false, the IsPartOf andIsAutoCreate annotations do not appear in the EDMX schema’s NavigationProperty.

The following table describes the behavior of the IsPartOf annotation depending on its value and the type of attribute it belongs to.
attribute IsPartOf="true" IsPartOf="false" IsAutoCreate="true" IsAutoCreate="false"
reference deep insert, parent item set to be the owner of the reference item not created, but existence is verified deep insert not created, but existence is verified
collection append with deep insert, parent item is set to be the owner of the collection item not created, but existence of all is verified append with deep insert not created, but existence of all is verified

Deep persistence examples

The following ImpEx script creates a product, MyProduct, with a unit attribute of the type Unit. In the type system, the Unit attribute's partOf value is set to false. The example ImpEx sets the attribute's autoCreate value to true.

INSERT_UPDATE IntegrationObject; code[unique = true]; integrationType(code)
; MyProduct; INBOUND

INSERT_UPDATE IntegrationObjectItem; integrationObject(code)[unique = true]; code[unique = true]; type(code)
; MyProduct  ; Product              ; Product
; MyProduct  ; Unit                 ; Unit

INSERT_UPDATE IntegrationObjectItemAttribute; integrationObjectItem(integrationObject(code), code)[unique = true]; attributeName[unique = true]; attributeDescriptor(enclosingType(code), qualifier); returnIntegrationObjectItem(integrationObject(code), code); unique[default = false]; autoCreate
; MyProduct:Unit                 ; code                    ; Unit:code                       ;                      ;   ;
; MyProduct:Unit                 ; name                    ; Unit:name                       ;                      ;   ;
; MyProduct:Unit                 ; unitType                ; Unit:unitType                   ;                      ;   ;
; MyProduct:Product              ; code                    ; Product:code                    ;                      ;   ;
; MyProduct:Product              ; name                    ; Product:name                    ;                      ;   ;
; MyProduct:Product              ; unit                    ; Product:unit                    ; MyProduct:Unit       ;   ; true

The autoCreate override in the preceding ImpEx adds the IsAutoCreate="true" annotation to the unit navigation property of MyProduct in the EDMX schema, as shown.

<?xml version='1.0' encoding='UTF-8'?>
<edmx:Edmx xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx" Version="1.0">
    <edmx:DataServices m:DataServiceVersion="1.0" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
        <Schema Namespace="HybrisCommerceOData" xmlns="http://schemas.microsoft.com/ado/2008/09/edm">
            <EntityType Name="Unit">
                <Key>
                    <PropertyRef Name="integrationKey"/>
                </Key>
                <Property Name="code" Type="Edm.String" s:IsUnique="true" Nullable="false"/>
                <Property Name="name" Type="Edm.String" IsLanguageDependent="true" Nullable="true"/>
                <Property Name="unitType" Type="Edm.String" Nullable="false"/>
                <Property Name="integrationKey" Type="Edm.String" Nullable="false" s:Alias="Unit_code"/>
            </EntityType>
            <EntityType Name="Product">
                <Key>
                    <PropertyRef Name="integrationKey"/>
                </Key>
                <Property Name="code" Type="Edm.String" s:IsUnique="true" Nullable="false"/>
                <Property Name="name" Type="Edm.String" IsLanguageDependent="true" Nullable="true"/>
                <Property Name="integrationKey" Type="Edm.String" Nullable="false" s:Alias="Product_code"/>
                <NavigationProperty Name="unit" Relationship="HybrisCommerceOData.FK_Product_unit" FromRole="Product" ToRole="Unit" Nullable="true" s:IsAutoCreate="true"/>
            </EntityType>
            <Association Name="FK_Product_unit">
                <End Type="HybrisCommerceOData.Product" Multiplicity="0..1" Role="Product"/>
                <End Type="HybrisCommerceOData.Unit" Multiplicity="0..1" Role="Unit"/>
            </Association>
            <EntityContainer Name="Container" m:IsDefaultEntityContainer="true">
                <EntitySet Name="Units" EntityType="HybrisCommerceOData.Unit"/>
                <EntitySet Name="Products" EntityType="HybrisCommerceOData.Product"/>
                <AssociationSet Name="Product_Units" Association="HybrisCommerceOData.FK_Product_unit">
                    <End EntitySet="Products" Role="Product"/>
                    <End EntitySet="Units" Role="Unit"/>
                </AssociationSet>
            </EntityContainer>
        </Schema>
    </edmx:DataServices>
</edmx:Edmx>

After overriding the type system, IsAutoCreate is added to unit in the returned EDMX schema. The unit’s IsAutoCreate annotation flag is now set to true, as shown.

As this example shows, when persisting a product for which a unit doesn’t exist in SAP Commerce Cloud, the system deeply persists the new unit first and then the product.
{
	"@odata.context": "$metadata#Products/$entity",
	"code": "my_new_product",
	"name": "My New Product",
	"unit" : {
		"code": "new_unit_pieces",
                "unitType": "new_unit_pieces",
                "name": "new_unit_pieces"
	}
}

Deep persistence errors

Persistence happens in an all-or-nothing manner. If an exception occurs during persistence, the whole Integration Object, including all deeply-inserted dependent objects, rolls back. Additionally, dependent objects do not persist when the partOf attribute is set to false in the items.xml file.

Attempting to persist a new item to an attribute with false values for both partOf and autoCreate produces an error. Using the MyProduct example, when the Unit attribute's partOf and autoCreate values are set to false, attempting to persist the Product with a new Unit returns an error, as shown:

{
    "error": {
        "code": "missing_nav_property",
        "message": {
            "lang": "en",
            "value": "Required NavigationProperty for EntityType [Unit] does not exist in the System."
        },
        "innererror": "my_new_product"
    }
}

For more information about errors, see Error Messages.