The data model of hybris is defined declaratively in items.xml. This file is like the nervous system of Hybris project. Every data definition starts from here. This file can be used to define any data structure used in project.
Below are few important points about items.xml
- The item type in itself, is of type Item.
- The instance of item type are stored is item type table, but the item type defination is stored in composedType. For example,for the Student item type, the instances of Student will be stored in Student table, but the definition of Student will be stored in composedType table.
- Any extension can have a items.xml file to define data model used in extension. But this file is not mandatory to have.
- The order in which the types are declared is very important. The order in which types are defined must conform to below
- Atomic types
- collection types
- enum types
- map types
- relation types
- item types
- GenericItem is the default parent of each item type.
- We can extend one item type using keyword
<itemtype code=”ApparelProduct” extends=”Product”>
Here ApparalProduct is extending the Product. This is similar to java inheritance concept. The subclass will have all properties of super class. In our case, all the attribute of super class are available for sub class.
- Ideally we can define an our custom item type, in any extension. But we should define it in relevant extension only.
- During build of hybris, the extensions are build with taking care of dependency, it makes sure that the parent item type is available.
- We can control the access to an attribute using modifiers.
<attribute autocreate=“true” qualifier=“code” type=“java.lang.String”>
<persistence type=“cmp” qualifier=“internalCode”/>
<modifiers read=“true” write=“false” search=“true” initial=“true” optional=“false” unique=“true”/>
Here the code attribute, is readable, but not writable. It means it’s value cannot be changed, after it is set once.
read – If false, we cannot access it from our java program.
write – If false, we cannot modify the value.
optional– if false, it is mandatory to initialize this attribute. Since it is not optional.
unique– if true, the attribute must hold a unique value. Similar to unique constraints in DBMS.
search – if true, the attribute is searchable through queries.
- We can control, how the tables are created for an item type. The 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”/>
Here the table name will be UserRights. The type code is used internally by hybris to generate primary keys. We can give any positive integer for typecode, barring few reserved integers.
- If we do not give a deployment tag for a item type, the instances of that item type will get start to store in Generic item table. This will cause generic item table to expand drastically, which is a not good for database performance. So we should always give deployment, if our item type is extending generic item directly.
- 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.