16 Hybris DataModel
16 Hybris DataModel
16 Hybris DataModel
Step 2 = D-Click / RMB on Jar file & Continue the installation by clicking → Next → Next…
Note: - Change default installation path (C:\Program Files\squirrel-sql-3.7.1) to “C:\tools” & finish
installation
Step 3 = Download HSQL Driver from below link: -
https://sourceforge.net/projects/hsqldb/files/hsqldb/hsqldb_2_4/hsqldb-2.4.0.zip/download
Step 6 = Next time, You can simply click on “Squirrel SQL Client” & Connect.
Expand HSQLDB (Created Connection) – PUBLIC --- PUBLIC – TABLES – Double click on any table to see the data
Contact Us = [email protected]
Explain Data model of Hybris?
✓ Hybris Data Modeling answer to Questions like: -
(1) How Hybris is going to handle the DB. (2) How can we create new tables.
(3) How can we create entire DB. (4) …
Ans: - Hybris Data Modeling: -
Helps you in developing your DB.
Can take care of the DB Connections & DB Queries. And …
Note: - If we don’t have “User Table (With Order List) then “How to get Order Count of each user?
Ans = select count (Order.OrderID), User.UserID from Order, User where User.UserID =
Order.UserID group by Order.UserID
Contact Us = [email protected]
Disadvantage with above = It will read all the records in Order table (Order table might
have Lacks records). Which is performance challenging.
Q: Tell me the logic which will give Orders count of the user without scanning / reading
entire Orders table?
Ans = Create User Table with Oder List & do below: -
select OList from User where UserID = 101;
Q: What will be the datatype of OList in Hybris? = collectiontype
Q: What is the default collection in Java = Collection.
Hence we need to specify this Collection as list / set.
List = Duplicates Allowed & Insertion Order is followed.
Set= Duplicates not allowed & Insertion Order depends on Implementation
We can create set collection / list collection / media collection / ...
Note: - Too much data normalization is not good. Bcoz joining is difficult & decrease performance.
o (3) Enum types = Used for preparing particular set of values (Example: - Days in Week)
Create a column which will accept only fixed set of values (Gender Column which accept M / F).
Oracle = create table ABC (Gender Varchar(10) check (gender in ('F','M'));
Similar, if you want to create similar in frontend, then in hybris we have – enumtypes.
o (4) Map types = Used to store key value Paris. Each key will have it’s own value.
If you want to create a column which accepts key & values pairs (Like Map) then in hybris we
have maptypes. Generally, we don't use maptypes. Bcoz we don't have column which store key,
values pairs. argumenttype = key & returntype = value
Example = Localized fields require separate value for each language.
✓ What are the Hybris Conventions for Deployment Codes (Must be +Ve & Range = 0 – 32767): -
o (1) commons extension (132XX) (2) print extension (234XX, 239XX)
o (3) processengine extension (327XX) (4) Legacy xpring extension (244XX, 245XX)
o (5) b2bcommerce extension (100XX)
Contact Us = [email protected]
Note: - Typecode values between 0 and 10000 are reserved for hybris-internal use. So, Typecode values
> 10000 are free for you to use.
Q: What happens if we don't specify deployment table?
Ans: - It will go & check what is base itemtype and so on.....then finally attributes stored in GenericItem
(Bcoz GenericItem is the Base itemtype).
This is not best option. Hence, have separate deployment tables every time.
In Hybris -- There won't be separate DB Designers.
We specify everything in XML, Hybris internally creates required tables.
Whatever name given for Code is used in Hybris & Whatever name given for table is used in DB level.
✓ Dynamic Attributes
✓ Dynamic Enums
✓ Multi-Tenant Support = Tenants allow customizing: - currency, currency format, data format.
Contact Us = [email protected]
Q: Explain *-items.xml?
✓ Q: How to create table in Oracle?
CREATE TABLE Persons (PID char(4), LNme varchar(255), FName varchar(255), Address varchar(255));
Note: - MySQL Data Types = BIT, DECIMAL, ENUM, INT, LongText and ...
Oracle Data Types = RAW, FLOAT(24), VARCHAR2, Number(10,0), Clob, Blob and ..
So, data types are very much important. Bcoz data types are changed from 1 DB to another DB.
Q: How do you manage if data types are different from 1 DB to another DB?
Ans: - Write separate script for each DB. This is not good practice.
Q: What is the Solution then?
Ans: - Write "Items.xml" -- It Contains the logic to create table & structure of table.
Hybris internally will create tables for you based on the DB from items.xml file.
General Syntax = extensionname-items.xml (Eg., \chennatrainingpayment-items.xml)
This file is not mandatory in all extensions.
Only some extensions will have Items.xml
Eg., → Eclipse, Press CTRL + SHIFT + R, Search = *training*items, You can see only 3 xml.
Note: - We have 2 important xml files for Learning: -
1) core-items.xml = We can't modify this as it's available in platform. To learn use this.
2) catalog-items.xml = We can't modify this as it's available in platform. To learn use this.
Note: - These are available in ...\hybris\bin\platform\ext\core\resources\core-items.xml
Q: To write XML, what we need to have / What is base? = XSD (XML Schema Definition).
Hence to write items.xml, we need to have items.xsd.
✓ Data model of Hybris are defined declaratively in items.xml (Used define data structure in project).
✓ Creates new data model from scratch as defined in "items.xml".
✓ The data model of extension is defined in <extension-name>-items.xml file
✓ Below are few important points about items.xml: -
✓ The item type in itself is of type Item.
✓ To define an items.xml file there must be an xsd file which has some rules.
✓ Instance of item type stored is item type table, but the item type definition is stored in
composedType. (Eg: - Student item type, instances of Student will be stored in Student table, but the
definition of Student will be stored in composedType table).
✓ GenericItem is the default parent of each item type.
In Java super class for all classes is Object class.
Q: Why Object class is the super class for all the classes.
Ans: - To reduce the code duplication. Basic methods that every class should contain and they are
very basic features or functionalities that. Every class should have and support (like getting Class
name, String representation of Object, finalize, notify, equals etc...).
✓ We can extend one item type using keyword (Simpler to Java inheritance using extends).
Contact Us = [email protected]
✓ Note: - In Hybris for us, "GenericItem" is the super item for all the items. But Internally for hybris always
Item is super item for everything.
If we create new item type & not mentioned the extends, then by default it will extends "GenericItem".
Note: - Base item types: -
Item = <itemtype code="Item" extends="">
ExtensibleItem = <itemtype code="ExtensibleItem" extends="Item">
LocalizableItem = <itemtype code="LocalizableItem" extends="ExtensibleItem">
GenericItem = <itemtype code="GenericItem" extends="LocalizableItem"
✓ We can control the access to an attribute using modifiers (or) What are default Modifiers: -
✓ Example: - <modifiers read= “true” write= “false” search= “true” initial= “true” optional= “false”
unique=“true” private = “false” private = “false”/>
o read – If false, we cannot access it from our java program.
o write – If false, we cannot modify the value.
o optional – If false, it is mandatory to initialize this attribute. Since it is not optional.
o unique – If true, attribute must hold a unique value. Similar to unique constraints in DBMS.
o search – If true, the attribute is searchable through queries.
✓ We can control, how the tables are created for an item type. Default table for each item type is
GenericItem. The table is called as deployment in Hybris. So GenericItem is the default deployment.
✓ <deployment table=“UserRights” typecode=“29”/>
✓ Table name is UserRights. Type code (+Ve Integer) internally used by Hybris to generate primary keys.
✓ If we don’t give deployment tag for item type, instances of that item type will store in GenericItem
table. This will cause GenericItem table to expand drastically, which is not good for DB performance.
✓ Persistence is one of the property of item type attribute. If this property is set to property, it means
that attribute will be stored in database. If it is set to dynamic, the attribute will not be part of
database table. It is just calculated at run time. This is similar to calculated column in DBMS.
Contact Us = [email protected]
Contact Us = [email protected]
Q: Explain Dynamic Attributes? =
We need a column in Java level, but don't need in DB level. When we go with this scenario?
Ans: - Sometimes, you may have fields that are derived from other fields, and should only be done
programmatically, rather than having the state be persisted via serialization.
The transient keyword in java is used on class attributes / variables to indicate that serialization process of such
class should ignore such variables. A transient variable is a variable that cannot be serialized.
Hybris supports transient variables. Whenever you create attribute, we will have persistence option: -
Note: - Persistence tells whether you want to save the data / not.
If you put persistence = property → Means you can save the data into that column.
If you put persistence = dynamic → Means you will not able save data into that column.
Advantage with Dynamic Attributes = (1) Data will not be saved in DB as its dynamic. (2) The custom
logic is written once and used all the time wherever that attribute is required.
Q: Explain generate = “true”?
Generally, we will be having 3 layers = Presentation Layer (PL) , Business Layer (BL), Database Layer (DL).
PL = We have registration form. Q: In Java -- How we save data in DB whatever entered in PL registration form?
Ans: - From BL to DL → How will you store data = ORM / Hibernate.
Q: Who write hibernate class / Entity class (Which is equal to DB table)? = Developer
Contact Us = [email protected]
But in Hybris You no need to write Entry class. DB table & Entity class will be created based "items.xml"
by Hybris automatically.
Entity class in hybris = Model class / Model.java
For Product table, we will have having ProductModel.java
In modifiers level... If you give read = "true", Means it will only create getter method (no setter method).
If you give write = "true", Means it will only create setter method.
Q: In what cases, we will have only getter methods (No setter methods).
Ans: - Connection properties. (OR) Creation Time Column (It's only once). Latter it will not be modified.
Q: What is the alternate if we don’t have the setter method? = CTOR Injection.
Q: How to make a field as mandatory? = optional = "false"
Note: - If you want to return your column as part of Flexible Search then search = "true".
Q: How to give value while creation of the row itself. It can be changed only once. That is 1st time itself. Attribute
is writable only at initialization time? Ans: - initial="true"
Note: - Generally, it is used with the combination of write = "false". Because It can change only once.
Q: How to make particular column / attribute as primary key? → unique = "true"
Q: How to create composite Primary Key?
Ans: - Put unique = "true" for whatever columns you want. Internally it creates the Composite key.
Note: - <custom-properties> If you want to search on particular columns in hMC then put that column in
custom properties. For PK columns, this will be created by default.
Contact Us = [email protected]
Q: Let's say, you have a table in which we are storing some intermediate values. In this case POJO class is not
required. If you don’t want POJO class to be generated for the attributes then make generate = "false".
Note: - The concept of generate = "true" will be there throughout all the types.
Note: - <atomictype class="java.lang.Object" autocreate="true" generate="false"/> → For atomictype,
even we have generate = "true" / "false" does not matter.
<collectiontype code="ExampleCollection" generate="false"/> → For collectiontype, even we have
generate = "true" / "false" does not matter. No Meaning.
Only "enumtype & itemtype" only will affect with generate = "true" / "false". It will create *.java files.
Rest all the types it won't create *.java file.
autocreate = "true / false" =
1) itemtype level = If autocreate = true, means item will be created during initialization. Default is 'true'.
Let's say, we have 4 columns now in itemtype & latter you want to add some more columns in itemtype,
then make autocreate="false".
2) attribute level = If autocreate = true, attribute descriptor created during initialization. Default is 'true'.
Note: - By default, there are some tables with in hybris named as "atomictype, collectiontype,
itemtype...". When you make autocreate = "true" then in this tables there will be row created.
Q: Why to create Index? = Indexes are created for faster search.
Oracle Syntax = CREATE INDEX PIndex ON Persons (LName);
Hybris Syntax =
Note: - At the end of all the attributes, you can add this.
Q: attribute defaultValue =
Contact Us = [email protected]
Q: itemtype: -
1) code = "Item" = Item table is will be created. This is used in Hybris level.
2) extends = " / "ABC" / ... → Defines the class, which will be extended. Default is 'GenericItem'.
3) jaloclass = "XXX" = Specifies the name of the associated jalo class. Default is [extension-root-
package].jalo.[type-code] which will be generated if not existent.
4) deployment = "XXX" = It is Deprecated. Use separate deployment sub tag. All instances of this type will be
stored in a separated database table (<deployment table="MediaFolders" typecode="54"/>).
5) autocreate="true" = If 'true', the item will be created during initialization. Default is 'true'.
Let's say, we have 4 columns now in itemtype & latter you want to add some more columns in itemtype, then
make autocreate="false".
6) generate="false" = If 'true', the source code for this item will be created. Default is 'true'.
Let's say, U have a table in which we are storing some intermediate values. In this case POJO class is not required.
If you don’t want POJO class to be generated for the attributes then make "generate = "false""
7) abstract="true" = Marks type & jalo class as abstract. If 'true', the type can’t be instantiated. Default is 'false'.
Q: attribute: -
1) autocreate = If 'true', the attribute descriptor will be created during initialization. Default is 'true'.
Note: - By default, there are some tables with in hybris named as "atomictype, collectiontype, itemtype...".
When you make autocreate = "true" then in this tables there will be row created.
2) qualifier = In attribute, qualifier is the Column / Attribute name. Means, column name = totalDiscounts.
Qualifier of this attribute. Attribute qualifiers must be unique across a single type.
3) read = true & write = "true"
If you specify read = "true", Means it will only create getter method (no setter method).
If you specify write = "true", Means it will only create setter method.
4) search = "true" → If you want to return your column as part of Flexible Search then search = "true".
Defines if this attribute is searchable by a FlexibleSearch. Default is 'true'.
Contact Us = [email protected]
Attributes with persistence type set to 'jalo' can’t be searchable.
5) optional = "false" → It makes a field as mandatory.
Defines if this attribute is mandatory or optional. Default is 'true' for optional. Set to 'false' for mandatory.
6) <persistence> = Persistence tells whether you want to save the data / not.
If you put persistence = property → Means you can save the data into that column.
If you put persistence = dynamic → Means you will not able save data into that column.
Defines how the values of the attribute will be stored.
Possible values: 'cmp' (deprecated), 'jalo' (not persistent, deprecated)
'property' (persistent), 'dynamic' (not persisted).
7) generate = "true" → If 'true', getter and setter methods for this attribute will be generated during a hybris
Suite build. Default is 'true'.
Note: - The concept of generate = "true" will be there throughout all the types.
<atomictype class="java.lang.Object" autocreate="true" generate="false"/> → For atomictype, even we have
generate = "true" / "false" does not matter.
<collectiontype code="ExampleCollection" generate="false"/> → For collectiontype, even we have generate =
"true" / "false" does not matter. No Meaning.
Only "enumtype & itemtype" only will affect with generate = "true" / "false". It will create *.java files. Rest all
the types it won't create *.java file.
8) unique = "true" = It creates primary key column if unique = "true".
If 'true', the value of this attribute has to be unique within all instances of this type.
If there are multiple attributes marked as unique, then their combined values must be unique.
Q: How to create composite Primary Key? = Put unique = "true" for whatever columns you want.
Internally it creates the Composite key.
9) initial = "true" → We can give value while creation of the row itself. It can be changed only once. That is 1st
time itself. Attribute is writable only at initialization time?
If 'true', the attribute will only be writable during the item creation.
Setting this to 'true' is only useful in combination with write='false'. Default is 'false'.
10) defaultvalue = Configures a default value for this attribute used if no value is provided.
The default value is calculated by initialization and will not be re-calculated by runtime.
It is similar to oracle default constraint.
Configure a default value if you don’t change / override latter.
Contact Us = [email protected]
Q: What are the ways to define “Itemtypes” in Hybris?
✓ Define the new item type without extending any existing item type
✓ Define the new item type by extending it with existing item type
✓ Define the existing item type again with new attributes
✓ 1 table will be created for Products & 1 table will be created for Categories & 1 extra table will be created
for LinkItem (elements on both sides of the relation are linked together via instances of a LinkItem table)
✓ Each LinkItem instance stores the PKs of related items for each row. So every row of product with
associated category will have 1 row in LinkItem table with PK of Category and associated PK of Product.
✓ LinkItem instances are used internally by hybris.
✓ We just need to call getters and setters at the API level to set and get the values.
✓ Note: - Extra table will be created only for many to many relations
Contact Us = [email protected]
Note: - We know that Collection can be used as an alternative for 1 – M relation.
Collection Relation
Advantage Fast retrieval as it does not need any There is no chance of data truncation (no
Join to get the results. matter how big the size). It's bidirectional.
Disadvantage If collection size grows there is a Slow in retrieval due to join between the tables
possibility of data truncation. required
It's unidirectional
It's not searchable as there will be a
list of PKs only present in the column.
When to Use When we are sure that in our current Whenever collection size is bigger or there is a
& future requirements, we will not chance that it can grow bigger then go with
have many rows mapped for one side. Relation, bcoz there will be no data truncation.
It means collection size is small, Bcoz For M - N, we should go for Relation always.
it helps to achieve faster retrieval.
When not to Use Whenever the collection size is very When collection size is smaller to compensate
big as it can lead to data truncation. slow retrieval of Relation but in that case, we
need to negotiate with Bidirectional mapping.
Q: Explain what is Redeclare in items.xml? = In Java, we can a variable with same in in Parent & Child classes
(In this case, Parent variable will be inherited, but will be hidden as Child also having same variable). We can
also change variable data type in child with same variable name.
Similar to Variable Hiding in Java, In Hybris we have "Re Declaring" the attributes.
✓ Sometimes it is required to re declare the attribute in the child item type for various reasons.
✓ It could be to change the Data type of an attribute or make an attribute as read only etc.
✓ We can re declare the same attributes in the Child item type to change such behavior
✓ Change the modifier from read=true to read=false. We can make attribute as unique.
✓ We can add write=false. We can also change the type of the attribute but only to subtypes.
Contact Us = [email protected]
Q: Using a standard Hybris configuration, is this a valid item type definition?
<itemtype code="MyItem" abstract="false" extends="GenericItem"> </itemtype>
1. No, this item needs a deployment table
Q: When we should define deployment mandatorily?
1) Defining new item type by extending GenericItem (Above is the example).
2) Defining new item type by extending existing item type for which there is no deployment
Q: What is the recommended way to create a new extension within Hybris V6?
1. Use the installer script with the extgen recipe
2. Extensions are automatically created by the build framework based on your dependencies
3. Use the build framework with the extgen ant target to create a new extension from a template
4. Use the build framework with the extgen maven goal to create a new extension from a template
Contact Us = [email protected]
Hybris DB Internals - Creating tables?
• Hybris creates tables during INIT / Update. It loads old-style ([-.]advanced-deployments.xml) and new
style data model definitions (-items.xml).
• Hybris generates DB schema using HybrisSchemaGenerator.initialize(). Schema is created as a set of
SQL files in temporary directory using Apache DDLUtils SQLBuilder. Hybris creates three files: -
o DropDDL = With Drop Table statements
o DDL = With CREATE statements (tables and indexes)
o DML = With INSERT / UPDATE statements
• The generated files are executed as a batch using Spring Core JDBC.
Table types: -
Contact Us = [email protected]