What is the PartOf modifier in Hybris items.xml?

What is PartOf?

PartOf modifier is used to define the aggregation relationship between Parent and Child objects. To explain it better I would say PartOf is used to defining cascade delete. When we delete a Parent object then all its child objects(partOf) will be deleted automatically.

Why or When to use PartOf?
PartOf may make sense when the relationship involves "is part of" description. For example, an OrderEntry record(AbstractOrderEntry) is part of its parent Order(AbstractOrder), and older entries will never be shared between multiple orders. If the Order were to vanish, the OrderEntry should as well, and an order entry without an Order would be a problem.

How to use PartOf?
On the removal of an order, if we want to remove all associated order entries automatically, then we can define partOf in its relation like
    <relation code="AbstractOrder2AbstractOrderEntry" localized="false" generate="true" autocreate="true">
        <sourceElement type="AbstractOrder" qualifier="order" cardinality="one">
            <modifiers read="true" write="true" search="true" optional="true"/>
            <custom-properties>
                <property name="ordering.attribute">
                    <value>"entryNumber"</value>
                </property>
            </custom-properties>
        </sourceElement>
        <targetElement type="AbstractOrderEntry" qualifier="entries" cardinality="many" collectiontype="list"
                       ordered="false">
            <modifiers read="true" write="true" search="true" optional="true" partof="true"/>
        </targetElement>
    </relation>
This also can be defined at the attribute level. Like
    <itemtype code="User"
              extends="Principal"
              jaloclass="de.hybris.platform.jalo.user.User"
              autocreate="true"
              generate="true">
        <deployment table="Users" typecode="4" propertytable="UserProps"/>
        <attributes>
            ...
            ...
            ...
            <attribute autocreate="true" qualifier="addresses" type="AddressCollection">
                <modifiers read="true" write="true" search="false" optional="true" partof="true"/>
                <persistence type="jalo"/>
            </attribute>
            <attribute autocreate="true" qualifier="carts" type="CartCollection">
                <modifiers read="true" write="true" search="false" optional="true" partof="true"/>
                <persistence type="jalo"/>
            </attribute>
        </attributes>
    </itemtype>
If we remove a user, its all addresses & carts will be removed.
Author Image

Ankitkumar Patel

Sr. SAP Hybris consultant, having 15+ years experience in SAP Commerce Cloud (Hybris), SAP Spartacus. Extensive experience in SAP Hybris development, third-party integrations, project architecture and design... Read more

Comments

  1. This is really useful article. Thanks!

    ReplyDelete
  2. Good explanation Ankit keep it up

    ReplyDelete
  3. Hello ,

    How i can change content GeneratedAbstractOrderEntry in hybris please if you have any ideas ?

    ReplyDelete
  4. Hi Ankit,
    I do have a question, in your first example i.e. OOTB code, if I want to make the partOf modifier as false then how we can achieve it.






    "entryNumber"






    ReplyDelete

Post a Comment

Popular posts from this blog

Hybris flexible search query examples

How to remove or update all data records in Hybris?

How to Install temporary Hybris license?