CRM and CX Blogs by Members
Find insights on SAP customer relationship management and customer experience products in blog posts from community members. Post your own perspective today!
cancel
Showing results for 
Search instead for 
Did you mean: 
Sagar_Bh
Participant
Hi There,

Welcome to the 3 blog post of SAP Commerce cloud.
As we have already discussed the basic building blocks of hybris, I would like to discuss DATA MODELLING in hybris today.

So, there are 2 things that you need to know :

1) Items.xml - Where you define the new item types or extend the existing item types

2) Items.xsd - items.xml file is validated against this XSD file. A type definition order that doesn’t conform to the items.xsd causes SAP Commerce to fail the extension build.

So, According to items.xsd,
there has to be a fixed structure that we need to follow while defining item types.:

 

<items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="items.xsd">

<atomictypes> </atomictypes>
<collectiontypes> </collectiontypes>
<enumtypes> </enumtypes>
<maptypes> </maptypes>
<relations> </relations>
<itemtypes> </itemtypes>
</items>

Let us go one by one:

Atomic Types: 


AtomicTypes are the most basic types available in the hybris.
They are the illustration of Java number and String item types, which include java.lang.Integer or java.lang.String.
We usually don't need to define new ones.


Syntax:

<atomictype class=“java.lang.String” extends=“java.lang.Object” autocreate=“true” generate=“false”/>

 

collection types:


Collection basically contains elements of the same type.
For example: A collection of String will contain the elements of String element type

Syntax:
<collectiontype code="AddressCollection" elementtype="Address" autocreate="true" generate="false"/>

<collectiontype code="StringCollection" elementtype="java.lang.String" autocreate="true" generate="false"/>


Enum Types:


This is used when we need Attributes with pre-defined values.


For Example, we need titles like Dr., Mr., Ms.

Syntax:

<enumtypes>
<enumtype code="enumSampleType" autocreate="true" generate="true" dynamic = "true">
<value code="sample1"/>
<value code="sample2"/>
<value code="sample3"/>
</enumtype>
</enumtypes>

You can set the dynamic="true" to allow adding values at runtime(by default, dynamic=” false”).

 

Map Types:


 Map Type is a collection of key-value pairs.

For each key (referred to as argument), there is a corresponding value (referred to as return type).

Syntax:
<maptype code="addressMap"  argumenttype="java.lang.String"  returntype="Address"                 autocreate="true"  generate="false"/>


A very common use of MapTypes is localized values - values that may differ in every language available in the system, like product descriptions in German and English.


Relation types:



Relation types help us to maintain the m:n relation between 2 tables.


If you delete an item from a relation, neither the item you seem to delete nor its related item is deleted, only the LinkItem is removed. In other words, both the source item and the target item remain, only the link between them is removed.

there are 2 types of relations hybris supports one to many & many to many.


Extra table will be created only for many to many relations.

Syntax:
<relation code="CategoryProductRelation" autocreate="true" generate="true" localized="false">
<deployment table="Cat2ProdRel" typecode="143"/>

<sourceElement qualifier="supercategories" type="Category" cardinality="many" ordered="false">
<description>Super Categories</description>
<modifiers read="true" write="true" search="true" optional="true"/>
</sourceElement>

<targetElement qualifier="products" type="Product" cardinality="many" collectiontype="list" ordered="true">
<description>Products</description>
<modifiers read="true" write="true" search="true" optional="true"/>
</targetElement>
</relation>

Relations are preferred over Collections in case of need for back traceability.

Item Types


 

This is the main block where we define the table and its attribute details.

There are 3 ways to configure:

1) Define the new item type without extending any existing item type
<itemtype code="New" autocreate="true" generate="true">

<deployment table="New" typecode="11000" />

             <attributes></attributes>

2) Define the new item type by extending it with the existing item type
<itemtype code="NewAddress" extends="Address" autocreate="true" generate="true"

             <attributes></attributes>

3) Define the existing item type again with new attributes

      <itemtype code="Address" autocreate="false" generate="false"

             <attributes></attributes>



AUTOCREATE- create a new database entry for this type at the initialization/update process. (set it to true for the first definition of item type)

GENERATE- to generate a new jalo class for this type during build time

DEPLOYMENT TABLE- specifies the table name.

TYPECODE- a unique number that ranges between  10000 and 32767.


After defining the item types we need to add attributes inside the <attributes> tag.


<attributes>


<attribute qualifier="password" type="java.lang.String" >

                        <modifiers unique="false" encrypted="true"/>

                       <persistence type="dynamic" attributeHandler="customerSiteAge"/>

                    </attribute>

<attributes>



There are various modifiers, some of which are mentioned below:


Read- attribute is readable

Write-  attribute is writable

Search- attribute is searchable by flexibleSearch

Optional- attribute is mandatory to fill while saving the model (By default true).

Unique - to make sure field values are unique always

Encrypted - to encrypt data before saving it to the database

Persistence type - To store data inside the database or just retrieve a value based on some logic. ( we define attrbiuteHandler which basically is a class that extends AbstractDynamicAttributeHandler)

Redeclare- If you want to redeclare or change any modifiers of an attribute.


For more information, you can read on help.sap.com




and if any queries please feel free to drop comment below.

Thanks for the Read.

Signing off!

Sagar Bhambhani.