1.03 Data Modeling - SAP Commerce Cloud Developer Training
1.03 Data Modeling - SAP Commerce Cloud Developer Training
Installation
Validation Security
Process Configuration
AddOn Service
Engine
Event Update and
Cache
System Initialization
Backoffice OCC Workflows
Scripting
2
The Context
3
Run
Preparation
Script
Class
Java is an is a
instance blueprint
of Object of
Type
SAP Commerce is an is a
instance blueprint
of Item of
5
Java Classes vs SAP Commerce Types
Car
is represented as fuel : Enumeration
CarModel.java
kwatt : Double metatype
by runtime, is represented
as an instance of
code : HistoricCar
name : Some Old Car
fuel : Gasoline
kwatt : 30
6
Types used in SAP Commerce
AtomicType
▪ Represents Java value objects which are mapped to database types
− Java Primitive keywords: int
− Java Wrapper Classes: java.lang.Integer, java.math.BigInteger
− Some Reference types: java.util.Date, java.lang.String, de.hybris.platform.core.PK
CollectionType
▪ Represents a typed collection
EnumerationType
▪ ComposedType which describes enumerations
7
Types used in SAP Commerce
MapType
▪ Represents a typed Map
RelationType
▪ Used to model binary dependencies between items, representing n:m relations.
8
The sections within extensionName-items.xml (in order)
<items>
</items>
9
Extending the Data Model
▪ Create new types:
− Define a type by extending already existing types, such as:
<itemtype code="Car" extends="Product">
10
Extending the Data Model • Extend Existing Type
Product
Product code vehicle01
+ code: String version Catalog
name F40 + name: String
1 Version
+ brand: String
version Online
brand Ferrari
Car
autocreate
▪ Add attribute definitions to existing types (attribute injection) If set to true, indicates this is the first
mention of this ItemType, which should be
<items> created during initialization. As Product is
<itemtypes> already defined in Commerce, set to false.
11
Extending the Data Model • New Type
Product
Car code vehicle01 version
+ code: String Catalog
name F40 + name: String
+ brand: String 1 Version
version Online
brand Ferrari
hp 300
Car
kw 212
+ hp: int
+ kw: int
<items>
<itemtypes>
<itemtype code="Car" extends="Product" autocreate="true" generate="true">
<attributes>
<attribute qualifier="hp" type="java.lang.Integer">
<description>Horsepower</description>
<persistence type="property"/>
</attribute> What is the difference
<attribute qualifier="kw" type="java.lang.Integer">
<description>Kilowatt</description>
between property and
<persistence type="dynamic" dynamic?
attributeHandler="kwPowerAttributeHandler"/> In what conditions will
<modifiers write="false" />
</attribute>
dynamic be used?
...
Extending the Data Model • Enumerated Types
Product
Car code vehicle01
+ code: String version Catalog
name F40 + name: String
+ brand: String 1 Version
version Online
brand Ferrari
hp 300
Car
kw 212
+ hp: int
fuel gasoline + kw: int
+ fuel: enumeration
<items>
<enumtypes>
<enumtype code="FuelEnumeration" generate="true" autocreate="true" { gasoline | diesel | ethanol }
dynamic="true">
<value code="diesel"></value>
<value code="gasoline"></value>
<value code=”ethanol"></value>
</enumtype>
</enumtypes>
<itemtypes>
<itemtype code="Car" extends="Product" autocreate="true" generate="true">
<attributes>
...
<attribute qualifier="fuel" type="FuelEnumeration">
<persistence type="property"></persistence>
</attribute>
Extending the Data Model • Composed Type References
Product
Car code vehicle01
+ code: String version Catalog
name F40 + name: String
+ brand: String 1 Version
version Online
brand Ferrari
hp 300
Car mechanic
kw 212
+ hp: int 0..1
fuel gasoline
+ kw: int
User
mechanic Montgomery Scott + fuel: enumeration
<items>
<itemtypes>
<itemtype code="Car" extends="Product" autocreate="true" generate="true">
<attributes>
...
<attribute qualifier="mechanic" type="Employee">
<modifiers read="true" write="true" search="true" />
<persistence type="property" />
</attribute>
...
Extending the Data Model • Relations
Product
Car code vehicle01 User code Alain Prost
+ code: String version Catalog
name F40 team Ferrari + name: String
+ brand: String 1 Version
version Online ... ...
<items>
...
<relations>
<relation code="Car2EmployeeRelation"
localized="false" autocreate="true" generate="true">
<sourceElement qualifier="car" type="Car" cardinality="one" />
<targetElement qualifier="drivers" type="Employee" cardinality="many" /> This example is 1:many, but
</relation> many:many relations are also
</relations> supported
<itemtypes>
...
2.16 (Data Mapping)
update
ext-items.xml
DB tables
16
Collections and Relations
<collectiontype code="StringCollection"
elementtype="java.lang.String" autocreate="true" />
<collectiontype code="LanguageCollection"
elementtype="Language" autocreate="true"/>
...
18
Relations
Category Product
products points to points to supercategories
(List<Product>) (List<Category>)
CategoryProductRelation
products is added supercategories is added
to the Category type
source (Category) to the Product type
as an attribute target (Product) as an attribute
20
Deployment
What is the main incompatibility between object-oriented programming and relational databases?
22
Object Relational Mapping • Storing objects in the DB
▪ By default, items for a given type are stored in the same database tables as its supertype
▪ Specify any item type's deployment to store its items in its own db tables.
▪ SAP Commerce recommends that deployment be specified for the first layer of GenericItem subtypes
– Consider carefully the performance implications of specifying deployment for other item types
– Set build.development.mode = true in local.properties to mandate that all direct children of GenericItem
have deployment specified.
GenericItem genericitems
products
MyType Product
mytypes
Car
23
O-R Mapping • Deployment Example
GenericItem genericitems
products
MyType Product
mytypes
cars
Car
PK P1 P2 P3 C1 C2 C3 products
…
Product
… … … … … …
Products
Car
GenericItem
PK P1 P2 P3
… … … …
products products
Product
PK P1 P2 P3 C1 C2 C3 cars
… … … … … … …
Car
cars
25
O-R Mapping • Attributes of a (Composed) Type
products
code vehicle01
users
26
O-R Mapping • Deployment of Relations • 1
▪ One-to-Many
− Additional column at the many side which holds the PK of the One side
− Users table from the example below would have an additional column car
▫ As with Car, Employee type does not have its own deployment, so its items live in the parent type's table Users
PK name car …
8796128083972 André-Marie Ampère 5553620010023 …
PK code …
5443669878887 C.-A. de Coulomb 5553620010023
8796256894977 vehicle01 …
USERS table
5553620010023 rocket05 … (holds User and Employee items)
PRODUCTS table
(holds Product and Car items) 27
O-R Mapping • Deployment of Relations • 2
A deployment must be specified
▪ Many-to-Many for many-to-many relationships
− New database table which holds the source and target PKs
<relation code="Product2ReviewerRelation" autocreate="true" generate="true"
localized="false">
<deployment table="Prod2ReviewerRel" typecode="20123"/>
<sourceElement qualifier="reviewers" type="Employee" cardinality="many" >
<modifiers read="true" write="true" search="true" optional="true" />
</sourceElement>
<targetElement qualifier="products" type="Product" cardinality="many" >
<modifiers read="true" write="true" search="true" optional="true" />
</targetElement>
</relation>
▪ Collections
− Stored in one database column
− Comma separated list of PKs or Atomic Values
<collectiontype code="StringCollection"
elementtype="java.lang.String" autocreate="true" />
<collectiontype code="LanguageCollection"
elementtype="Language" autocreate="true" />
...
products
29
Type System Localization
▪ Service layer provides support for i18n So, who needs what?
− Your data values (Itemtype properties) Front-end customers around the globe
31
Type System Localization • Type-Name and Attribute-Name Display Labels
32
Localize Types during System Initialization or Update
▪ To read Type-definition localizations from .properties files and store them in the database
metadata tables:
▪ Overrides type localizations in the database with the ones from the locales_XY.properties files
33
Localizing Attribute Values For front-end customers
around the globe
▪ Any itemtype property may be localized 1. Define in *-items.xml
2. Populate in Backoffice or
<itemtype code="Product"> ImpEx
<attribute qualifier="barcode" type="java.lang.String" /> 3. Displayed in storefront
<attribute qualifier="quote" type="localized:java.lang.String" /> based on customer's locale
34
Enabling Localized Data Types
▪ The localized: prefix is not a keyword; localized types must be defined in *-items.xml
– A <maptype> entry exists for each OOTB localized type, defined in the core extension's core-items.xml
▪ For example
<itemtypes>
<itemtype code="Car" extends="Product">
<attribute qualifier="ownerManual" type="localized:Booklet" />
... If we wanted to define
</itemtype>
a localized user's
<itemtype code="Booklet" extends="Product">
... manual for our car…
</itemtype>
...
<itemtypes>
<maptypes>
<maptype code="localized:Booklet" argumenttype="Language" We must also define the
returntype="Booklet" autocreate="true" generate="false"> localized:Booklet
...
maptype
<maptypes>
35
Demo
References
▪ https://help.hybris.com/latest/hcd/8c755da8866910149c27ec908fc577ef.html
▪ https://help.hybris.com/latest/hcd/8ecae959b9bd46b8b426fa8dbde5cac4.html
37
The SAP Commerce type system is used to model system in an abstract way.
All XML type definitions are converted during the system build into corresponding java classes, which will be
used at runtime.
Item types convert to models – the foundation/entities of the Commerce Suite's type system.
Each model is comprised of attribute and relation metadata, an ID, a DB table and a supporting Java class.
Always update/initialize SAP Commerce to apply any type-related changes to the database.
If possible, try to use relation types rather than collections
The deployment tag deploys the current type to its own table instead of using the table of its super type.
▪ Understand the performance impact
Localization in SAP Commerce is two-fold:
▪ Types and attributes description localization – useful for Backoffice users
▪ Localized attributes – seen by storefront customers based on their locale
38
Exercise 2
39
Thank you.