SAP CX | Hybris

This space is to help software developer who wants to learn Hybris, now known as SAP Customer Experience (or SAP CX in short). Focus is to help newbie in developing understanding around some basic things e.g. SAP CX (Hybris), Java, Spring and help them in solving common problems that they face in real world. All the examples and solutions provided here are based on Windows OS.

At the same time, people can find here some of the common interview questions asked to Hybris developer.

Some of the common topics covered are listed below.

Groovy Script To Generate GUID (Globally Unique Identifier) For A Hybris Item

import de.hybris.platform.commerceservices.util.GuidKeyGenerator
import de.hybris.platform.core.Registry

final GuidKeyGenerator guidKeyGenerator = (GuidKeyGenerator) Registry.getApplicationContext().getBean(“guidKeyGenerator”)
print(guidKeyGenerator.generate().toString))

Product Modelling In Hybris

In SAP Commerce, there are two ways of product modelling –

  1. Using Type System
  2. Using Classification System
  3. Hybrid modelling i.e. using a combination of Type System and Classification System.

Type System

SAP Commerce uses a system of types to organize data, for example product information, customer data, addresses, or orders.

Types define persistent objects in several aspects:

  • attributes manage and store data for the object,
  • the deployment defines the database table the object is stored in
  • the Java class of the object.

Types used in Hybris

  1. Atomic Type – Represents Java value objects which are mapped to database types.
    • Java Primitives: int
    • Wrapper: Integer
    • Some Reference types: java.util.Date
  2. Collection Type – Represents a typed collection.
  3. Map Type – Represents a typed Map.
  4. Composed Type – Composed object of other hybris types.
  5. Enumeration Type – ComposedType which describes enumerations.
  6. Relation Type – ComposedType which describes binary relations between items.

More details about type system is available here.

Classification System

Classification enables a developer to define product attributes in a way different to the typing method.
Classification-based attributes are called category features, sometimes also referred to as classification attributes.

Classification helps simplify the management of product data independently of product catalogs or product definitions. A classification system preferably takes the form of a hierarchical structure characterized by an appropriate number of levels of detail according to the range of product data to be covered. A classification system’s independence from product catalogs, combined with the ability to inherit features from higher levels within its classification structure, makes it easy to assign product-related attributes.

Structuring Classification Systems

In the platformbackoffice extension, a classification system takes a form similar to that of ordinary product catalogs, that is, a classification system has one or more classification system versions that themselves contain any number of classifying categories organized in a tree-like structure. Classifying categories also represent the layer where classification systems and product catalogs interface.

The following diagram shows how the SAP Commerce catalogs organize classification data:

SAP Commerce Classification Overview
A classification attribute, referred to as category feature in platformbackoffice, is part of a classification system and is a loose combination of the following:

  • An attribute label, referred to as feature descriptor.
  • A feature descriptor type, which specifies the kind of value.
  • Optionally, pre-selected values allowed for the classification attribute, referred to as feature values.

Category features are grouped in classifying categories, which are grouped in classification systems through the classification system versions.

The following model shows the main classification API classes along with some of the basic catalog classes.

Steps To Create Classification Class And Linking Product With Classifying Category
To define classification class, calssifying category and category feature, please refer categories-classifications.impex available at hybris/bin/modules/b2c-accelerator/electronicsstore/resources/electronicsstore/import/sampledata/productCatalogs/electronicsProductCatalog/categories-classifications.impex.

We can define calssification class, classifying category and category feature using impex as below.

  1. Create macro for impex as below.
    #Macros / Replacement Parameter definitions
    $productCatalog=yourProductCatalog
    $productCatalogName=Your Product Catalog
    $catalogVersion=catalogversion(catalog(id[default=$productCatalog]),version[default=’Staged’])[unique=true,default=$productCatalog:Staged]
    $classCatalogVersion=catalogversion(catalog(id[default=’YourClassification’]),version[default=’1.0′])[unique=true,default=’YourClassification:1.0′]
    $classSystemVersion=systemVersion(catalog(id[default=’YourClassification’]),version[default=’1.0′])[unique=true]
    $class=classificationClass(ClassificationClass.code,$classCatalogVersion)[unique=true]
    $supercategories=source(code, $classCatalogVersion)[unique=true]
    $categories=target(code, $catalogVersion)[unique=true]
    $attribute=classificationAttribute(code,$classSystemVersion)[unique=true]
    $unit=unit(code,$classSystemVersion)
    $baseProduct=baseProduct(code,$catalogVersion)
    $approved=approvalstatus(code)[default=’check’]
  2. Impex To Create Category
    INSERT_UPDATE Category;code[unique=true];allowedPrincipals(uid)[default=’customergroup’];$catalogVersion
    ;1
    ;1354
    ;1355
  3. Impex To Create CategoryCategoryRelation
    INSERT_UPDATE CategoryCategoryRelation;$categories;$supercategories
    ;1354;1355
    ;1355;1
  4. Impex To Update Category
    UPDATE Category;$catalogVersion;code[unique=true];name[lang=$lang];description[lang=$lang]
    ;;1;Category A;
    ;;1354;Category B;
    ;;1355;Category C;
  5. Impex To Create Classification
    #ClassficationClass extends Category
    #ClassificationClass has an attribute catalogVersion of type ClassificationSystemVersion.
    #ClassificationClass has an attribute classificationAttributes of type ClassificationAttributeList.
    #classificationAttributes is the list of all attributes available within this ClassificationClass.
    INSERT_UPDATE ClassificationClass;$classCatalogVersion;code[unique=true];allowedPrincipals(uid)[default=’customergroup’]
    ;;yourClassificationCode;;
  6. Impex To Create ClassificationAttribute
    INSERT_UPDATE ClassificationAttribute;$classSystemVersion;code[unique=true]
    ;;yourClassificationAttribute
  7. Impex To Link ClassificationClasses To Categories
    INSERT_UPDATE CategoryCategoryRelation;$categories;$supercategories
    ;1354;1316
  8. Impex For ClassAttributeAssignment
    INSERT_UPDATE ClassAttributeAssignment;$class;$attribute;position;$unit;attributeType(code[default=string]);multiValued[default=false];range[default=false];localized[default=true]
    ;1316;yourClassificationAttribute;0;;;;;
  9. Impex To Create ClassificationAttributeUnit
    INSERT_UPDATE ClassificationAttributeUnit;code[unique=true];symbol;unitType;name[lang=en];;;$systemVersion[unique=true]
    ;142;ml;142;milliliter;mililitre;Milliliter

Steps To Import Hybris Project In Intellij

  1. Open Intellij
  2. Open Settings… dialog (press Ctrl + Alt + S or File->Settings…), go to “Plugins” and search and install “SAP Commerce Developers ToolSet” as shown below.


  3. Restart Intellij and click on File->New->Project…
  4. Select “Empty Project” and then click “Next” as shown below.


  5. Give project name as below and click on finish.
  6. Close the popup, which comes automatically.
  7. Click File->New->”Project From Existing Resource” and select bin folder from your project folder as shown below.
  8. In the popup, select radio button “Import project from external model” and then select “hybris” as shown below.
  9. Click on “Next two more times and selects the project settings for import as shown below.
  10. Click on “Next” and then it shows all the extensions that will be imported in your project. It is shown below.
  11. Click on “Next” and select the JDK as shown below and then click on finish. It will open the “Intellij” workspace and a popup will come. Select “This Window if you want to open the project in same window or select “New Window“. After this step, your hybris project is ready to use in Intellij.