Data Modeling

In the world of SAP Hybris, data modeling is like the blueprint of your application. It’s what gives your app its structure and defines how everything is organized. Think of it as the foundation upon which your business logic stands.

Data models looks like the backbone for your application’s inner workings. They not only help you organize and manage your database efficiently but also play a crucial role in defining how your business processes flow.

In SAP Hybris, every extension has its own special file called (extension_name)-items.xml. This file is where the magic happens—it’s where you define your data models. Whether you’re working on customer data, products data, custom data, or anything else, it all comes together in this file.

Hybris use Type for design data modeling. Basically Types define an objects that used for manage and store object data with Java implementation in database.

Following types supported for data modeling :

Atomic types − It is used to create various Atomic types like Java number and string objects

Collection types − It is used to create Collections.

Enum types − It is used to define Enums(Constant type of object).

Map Types − It is used to define maps type of object to store object in key values pairs.

Relation types − It is used to create relation between tables.

Item types − It is used to create tables by defining model and table attribute.

About /<extension-name>-items.xml :

*-Items.xml file is locate resource/<extension-name>-items.xml file in each extension and responsible for create new and update existing data model related changes.

For Example :

At each build time items.xml file is validated against an XSD file (items.xsd). This XSD file called as schema file, in this file the order of type definitions are defined by SAP OOTB. Which of the definition order that doesn’t conform to the items.xsd causes SAP Commerce to fail the extension build.

Basic Structure of items.xml

<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>

for example Items Type looks like :

Example 1 :

<itemtype code="CSOrderEntry" extends="AbstractOrderEntry"  jaloclass="de.hybris.platform.jalo.order.OrderEntry"
            autocreate="true" generate="true">
   <deployment table="CSOrderEntries" typecode="46"/>
   <attributes>
       <attribute autocreate="true" redeclare="true" qualifier="order" type="Order">
           <modifiers read="true" write="false" search="true" removable="true" optional="false" initial="true"      unique="true"/>
       </attribute>
   </attributes>
</itemtype>

Example 2 : 

<itemtype code="Cart"
                extends="AbstractOrder"
                jaloclass="de.hybris.platform.jalo.order.Cart"
                autocreate="true"
                generate="true">
         <deployment table="Carts" typecode="43"/>
         <attributes>
            <attribute autocreate="true" redeclare="true" qualifier="entries" type="CartEntryCollection">
               <modifiers read="true" write="true" search="true" removable="true" optional="true" partof="false"/>
            </attribute>
            <attribute type="java.lang.String" qualifier="sessionId">
				<persistence type="property"/>
				<modifiers read="true" write="true"/>
			</attribute>
         </attributes>
      </itemtype>

Few Terminology used in Data Modeling :

code We use this as an identifier of the ItemType
extends This is useful for the superclass of this ItemType
jaloclass use to provide fully qualified classpath of this ItemType
autocreate If we set this true, then ItemType will be created when the platform creates the type system during initialization.
generate If we set this true, the platform creates getter and setter methods for this ItemType.
qualifier Used to provide identifier of the Attribute
typecode As a typecode we specify the unique number that will be used internally by Hybris as table reference.
redeclare Used to change the behaviour of an existing attribute of parent class into child class.
autocreate If we set this true, then Attribute will be created when the platform creates the type system during initialization.
type We use this to provide identifier of the type this for which attribute is going to be created.
write If we set modifier to true results in a setter method being generated for this attribute and while we set modifier to false results in no setter method being generated for the attribute.
read If we set modifier to true results in a getter method being generated for this attribute and while we set modifier to false results in no getter method being generated for the attribute
unique if we set as true, then it's mandatory that attribute must hold a unique value.
search This contains a boolean value as true/false.Default value is true (here true means attribute can be searchable by a FlexibleSearch and will act opposite for false)
optional This contains a boolean value as true/false.Default value is true (here true means attribute is mandatory and will act opposite for false).
persistent type For this the value can be set property or dynamic (if we set property then value will be stored in DB and if we set as dynamic then value will not be stored in DB and it’s called dynamic attribute).

In hybris there are two ways for set the database column type :

1. <persistence type="property"/>

2. By define more info about database systems as below :
       We can define size of column for diffrent database system.

<persistence type="property">
   <columntype database="oracle">
       <value>CLOB</value>
   </columntype>
   <columntype database="mysql">
       <value>text</value>
   </columntype>
   <columntype>
       <value>varchar(4000)</value>
   </columntype>
</persistence>

About deployment tag :

The deployment tag is used to define the table name and typecode into which the instances of the type are written, such as <deployment table=”CSOrderEntries” typecode=”46″/> .

Please Click here to know more about Deployment tag of items.xml.

Leave a comment