Home » Data model (DataTypes) » RelationTypes

RelationTypes

  • To create dependency between 2 types is called relation between the types.
  • Relation is basically used between 2 itemtypes in SAP Commerce Cloud. So each of them can also access the instance of its partner itemtype. It’s a relation between 2 table in the terms of Database.

One to Many Relation:

User and Order are itemtypes. Also we can use the term UserModel and OrderModel in further discussion.

User and Order is having one to many relation. Which means :

  • User(UserModel) can access it’s Orders(List Of Orders)

Example : Create a one to many relation between User and Orders.Which means one User can have relation with many orders. Following is the example of one to many relation.

<relation code="User2Orders" generate="true" localized="false" autocreate="true">
    <sourceElement type="User" cardinality="one" qualifier="user">
        <modifiers read="true" write="true" search="true" optional="false"/>
    </sourceElement>
    <targetElement type="Order" cardinality="many" qualifier="orders">
        <modifiers read="true" write="true" search="true" optional="true" partof="true"/>
    </targetElement>
</relation>

Explanation:

There are Two elements are used to create  a relation: sourceElement  and  targetElement.
where
sourceElement is  User, targetElement is Order.
User’s cardinality is One and Order’s cardinality is Many. So this relation called “One to Many Relation”

One-to-many relations (1:n)

One User can have many orders:

onetoManyRelation

Many-to-Many relations:

Let’s Take an example of Products and Promotions.
the relation between both items are many to many relation.

Explanation:
1.One product can have many promotions or discounts.

mtom1


2.One promotion can be applied on many products.

mtom2

Syntax:

<relation code="ProductPromotionRelation"
   autocreate="true"
   generate="true"
   localized="false">
   <deployment table="ProdPromRelations" typecode="5018"/>
   <sourceElement qualifier="products" type="Product" cardinality="many">
      <description>Products</description>
      <modifiers read="true" write="true" search="true" optional="true"/>
   </sourceElement>
  
<targetElement qualifier="promotions" type="ProductPromotion" cardinality="many">
      <description>Promotions</description>
      <modifiers read="true" write="true" search="true" optional="true"/>
   </targetElement>
   
</relation>