Hybris Data Model

SAP Hybris Data Models

SAP Hybris - Modelling: The data model is base of application. Data model is define the structure of your application. Business logic is always based on top of the data model and helps an organize and maintain their database. In Hybris, each extension has <extension-name>-items.xml file. Data Models are define in same file.

Type System

Type system is used for design data modeling or organize data in Hybris. Types define an objects for manage and store data with Java implementation . For instance, Java have class and object . Class is blueprint of Object and Object is instance of Class. Same concept is follow in Hybris. Hybris is define Type and Item. Type is blueprint of Item and Item is instance of type.

JAVA: Class is blueprint of Object <-> Object is instance of Class.

HYBRIS: Type is blueprint of Item <-> Item is instance of Type.

Type = items.xml + Java implementation

There are two kind of Types

  1. System Related Type : It is extends type system itself and deal with manage internal data

    • Data Type : It is used for define attribute value and representation type as CollectionTypes, MapTypes, EnumerationTypes, and AtomicTypes . It is same like in Java Date , Map , Enum etc.

    • Infrastructure Type : It is called ItemType as well. It is carry attribute and hold information as ComposedTypes , Relation. It is same like Java Object.

2. Business Related Type : it is manage business activity data like Order, Customer, Product .

Hybris Types Available

    • Item types − This is used to create model (POJO) and tables in DB.

    • Relation types − This is used to create mapping between tables.

    • Atomic types − It is used to create as basic types in Hybris, which include Java number and string objects

    • Collection types − It is used to build collection/group of element types

    • Map Types −It is used to tore key values pairs in Hybris data modeling

    • Enum types − It is used to build enumeration for preparing a particular set of values

Item.xml

Data entities are defined with item type elements, whereas relations between items are defined with relation elements. Item.xml file is locate resource/<extension-name>-items.xml file in each extension. which is used for create data model of business . you can define new types ,override and extend existing types.

For example:

  • If extension name is core, the file is called core-item.xml.

  • If extension name is facade , the file is called facade-item.xml.

Basic Structure

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

items.xml file has maintain the above order of elements. because It is always validated against items.xsd. xsd file has defined the order in which the elements have to be follow same order in items.xml . if you will not follow same order. build will fail.

items.xml file is parsed and evaluated in running order in one single pass. The Hybris does not allow multi-pass processing of the items.xml file . This means that you need to define types in order of inheritance. More abstract types need to be defined more to the beginning of the items.xml file and more concrete types need to be defined more to the end. During a hybris build, the build process validate that every extension's /resources directory contains a copy of a items.xsd file . This main file allows the hybris to validate the extension's items.xml against the items.xsd file.

Atomictypes

AtomicTypes are the basic types in the Hybris Commerce Suite. It is the representation like Java number and String object types, such as java.lang.Integer or java.lang.String. When you define an AtomicType yourself, you need to assign a Java class object to it and it is generate upon platform initialization

<atomictypes>

<atomictype class="java.lang.Object" autocreate="true" generate="false"/>

<atomictype class="java.lang.Number" extends="java.lang.Object" autocreate="true" generate="false"/>

<atomictype class="java.lang.Integer" extends="java.lang.Number" autocreate="true" generate="false"/>


</atomictypes>

CollectionTypes

A CollectionType contains number of instances of types . It is based on the Java Collection class. you can use of the Collection class and some of its sub-classes (List, Set, and SortedSet) . There are two types of relations that you can build with CollectionTypes: one to many relations and many to one relations. Both kinds of relation are unidirectional.

<collectiontypes>

<collectiontype code = "ProductCollection" elementtype = "Product" autocreate = "true" generate = "true"/>

<collectiontype code = "LanguageList" elementtype = "Langauage" autocreate = "true" generate = "true"/>


<collectiontype code="AbstractOrderEntryList" elementtype="AbstractOrderEntry" autocreate="true" generate="false" type="list"/>


</collectiontypes>

If the CollectionType contains AtomicTypes, the values are stored as binary fields in the database. If it stores a collection of items, then those items' Primary Keys (PKs) are stored in the database in string form. As all the values of one CollectionType instance are stored as one single column in database so reading and writing the values is quite fast .

Collection Types have technical limitations

  • If a collection contains a number of PKs, the field value may reach the maximum length of field for the database and collection entries may get truncated. you can only store values of a certain length in that database field and every bit of information beyond that length gets lost.

  • As the database entry only contains the PKs and not the items themselves, you cannot run database searches on the entries directly.

  • If a single CollectionType instance has several AtomicType entries that match a search query, you are not able to detect the exact number of matches from the database directly.

EnumerationTypes

EnumerationTypes is very much similar as Enum concept in Java. EnumTypes are ComposedTypes and handle values only have a limited number of choices like Gender Male , Female etc. All EnumTypes values , which are define in application , are store one single database table. This table might become quite large when a lot of EnumTypes define.

<enumtype code="Gender" autocreate="true" generate="true" >

<value code="MALE"/>

<value code="FEMALE"/>

</enumtype>


<enumtype code="Gender" autocreate="true" generate="true" dynamic="true">

<value code="MALE"/>

<value code="FEMALE"/>

</enumtype>

Hybris provide to create dynamic type Enum by using dynamic="true".

Map Types

A MapType is a typed collection of key/value pairs.it is used to store key values pairs in Hybris data modeling. A very common use of MapTypes is localized values - values that may differ in every language available in the system, like product name in Spanish and English.

<maptypes>

<maptype code="localized:java.lang.String" argumenttype="Language" returntype="java.lang.String" autocreate="true" generate="false"/>


</maptypes>

Localized values are stored in a separate database table, whose name is composed of the name of the table the type is stored in, plus the suffix lp (short for localized property).

Example : sampletype table and localized table will be sampletypelp

RelationTypes

RelationTypes represent n:m relations in the hybris . You can link a one item to other item. LinkItems hold two attributes, SourceItem and TargetItem, that hold references to the respective item . LinkItem is helper type item which is linked together of source and target item.

<relation code="Country2RegionRelation" generate="true" localized="false" autocreate="true">

<sourceElement type="Country" qualifier="country" cardinality="one">

<modifiers read="true" write="true" search="true" optional="false" unique="true"/>

</sourceElement>

<targetElement type="Region" qualifier="regions" cardinality="many">

<modifiers read="true" write="true" search="true" partof="true"/>

</targetElement>

</relation>

Kind of relation

  • one-to-one, unidirectional : (attribute definition, such as Product instance - Unit instance)

  • one-to many, unidirectional : CollectionType

  • many-to-one, unidirectional : CollectionType

  • many-to-many, bidirectional : RelationType

ItemTypes

ItemTypes are the base of the hybris. Item types are used to create new tables or to update existing tables.

<itemtype code="OrderEntry"

extends="AbstractOrderEntry"

jaloclass="de.hybris.platform.jalo.order.OrderEntry"

autocreate="true"

generate="true">

<deployment table="OrderEntries" 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>

ItemType Modifiers

  • code :The identifier of this ItemType

  • extends :The superclass of this ItemType

  • jaloclass: The fully qualified classpath of this ItemType

  • autocreate : If set to true, this ItemType will be created when the platform creates the type system during initialization.

  • generate: If set to true, the platform creates getter and setter methods for this ItemType.

AttributeDescriptor Modifiers

  • qualifier: The identifier of this Attribute

  • redeclare: change its behaviour of an attribute

  • autocreate: If set to true, this Attribute will be created when the platform creates the type system during initialization.

  • type: The identifier of the type this attribute is going to be .

  • write : Setting modifier to true results in a setter method being generated for this attribute and setting modifier to false results in no setter method being generated for this attribute:

  • read: Setting modifier to true results in a getter method being generated for this attribute and setting the modifier to false results in no getter method being generated for this attribute:

Custom Types Columns

Hybris provide two ways for set the database column type :

By specifying the database column type in the item.xml file, such as:

<persistence type="property">

<columntype>

<value>VARCHAR</value>

</columntype>

</persistence>

You can also define this in more detail by specifying database systems such as:

<persistence type="property">

<columntype database="oracle">

<value>CLOB</value>

</columntype>

<columntype database="mysql">

<value>text</value>

</columntype>

<columntype>

<value>varchar(4000)</value>

</columntype>

</persistence>

Deployment tag

<deployment table="table_name" typecode="46"/>

  • The deployment attribute specifies the table name into which the instances of the type are written, such as table="mytype_deployment"

  • The typecode attribute must specify a unique number to reference the type. The value of the typecode attribute must be a positive integer between 0 and 32767 (2^15-1) and must be unique throughout the hybris Commerce Suite as it is part of the PK generation mechanism. Typecode values between 0 and 10000 are reserved for hybris-internal use, typecode values larger than 10000 are free for you to use.

ServiceLayer: Generates *Model.java source files (model classes) for all item types of configured extensions to the bootstrap/gensrc directory

Jalo Layer: Generates Generated*.java source files (item classes) for all item types of your extension to the gensrc directory of your extension. The Jalo layer in hybris is deprecated now.

Update Hybris System

Since we modified the Hybris Data Model or modifying an items.xml file. After Run "ant all" and start Hybris server. we must update the system, i.e. push the changes through to the database.

  • Go to Platform/Update in the hybris Admin Console (https://localhost:9002/hac) Select only the first box Update running system,

  • If type system is localization then run Update system by selecting only Localize types

Verify that the new type Item

  • Open the https://localhost:9002/backoffice and login as admin user.

  • Open System/Types and search by Name of item like My Order.