SAP Hybris Tutorial – Data Modeling | items.xml File | Unit 9
This is the very important part we need to know about the data modeling in Hybris.
SAP Commerce allows you to match your specific business needs through product and data modeling. Good understanding of the type system allows you to reflect in your application your business model. The bean and enum generation feature also serves that purpose.
The items.xml file specifies the types (classes) of items (objects). By editing the items.xml file, you can define new types or extend existing types. In addition, you can define, override, and extend attributes in the same way.
In this post, you will know how important the items.xml file is.
Contents of the post.
- Location
- Structure
- Explanation
- Best practice
Let’s go deep dive into each part.
1) Location
The items.xml is located in the resources directory of an extension. The items.xml files are prefixed with the name of their respective extension in the form of <extension name>-items.xml. For example:
- For the core extension, the file is called core-items.xml.
- For the catalog extension, the file is called catalog-items.xml.
2) Structure
The basic structure of the items.xml file is as XML format below:
<items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="items.xsd">
<atomictypes>
// This type define the core object of java system. Like Object, Number, Integer, String, Boolean, Float…
</atomictypes>
<collectiontypes>
// Define the collection type like: List, Set, …
</collectiontypes>
<enumtypes>
// Define enum type
</enumtypes>
<maptypes>
// Define the map with key and value
</maptypes>
<relations>
// Define the relationship between 2 item types (2 tables): one to one, …
</relations>
<itemtypes>
// Define the item type will be map to the table in database.
</itemtypes>
</items>
Because the items.xml file is validated against an XSD file (items.xsd), the order of type definitions must conform to this order. if you don’t conform to the order structure of the file, you will get the error while building the extension.
3) Explanation
There are many attributes in the items.xml file.
Let’s see the example below:
<maptype code="ExampleMap"
argumenttype="Language"
returntype="java.math.BigInteger"
autocreate="true"
generate="false"/>
–> Argument type is the key, and return type is the value for the map exampleMap code.
An other example to define a new Item type:
<itemtype code="Manufacturer" autocreate="true" generate="true">
<deployment table="Manufacturer" typecode="20001"/>
<attributes>
<attribute qualifier="code" type="java.lang.String">
<modifiers read="true" write="true" search="true" optional="false" initial="true" unique="true"/>
<persistence type="property" />
</attribute>
<attribute qualifier="name" type="java.lang.String">
<modifiers read="true" write="true" search="true" optional="false" initial="true" />
<persistence type="property" />
</attribute>
<attribute qualifier="address" type="Address">
<persistence type="property"/>
<modifiers write="true" read="true" search="true" />
</attribute>
<attribute qualifier="finalAddress" type="java.lang.String">
<persistence type="dynamic" attributeHandler="dynamicManuAddressHandler"/>
<modifiers write="false" read="true"/>
</attribute>
</attributes>
</itemtype>
Let’s discover each of their meaning.
code="Manufacturer"
–> The name of a new item type, and it must be a unique name.
autocreate="true" generate="true"
–> autocreate=”true”: the “Manufacturer” type is generated and added to the type system automatically during the initialization or update of the system. (a type could be known as a Jalo class that works at the low-layer for advanced customization)
–> generate=”true”: the Model class (e.g., ManufacturerModel
) and other associated classes are auto-generated during the build process. This model class works look like an entity of a table database.
<deployment table="Manufacturer" typecode="20001"/>
–> Table and typecode attributes have to be unique, typecode is the number with range 1 -> 32767
–> From 1 -> 10000 is the range typecode for the itemType of the build-in Hybris system, the remaining for the custom type.
–> Typecodes are used internally by the system to differentiate between item types.
<attribute qualifier="code" type="java.lang.String">
–> A new attribute name as “code” with type as “String”
<modifiers read="true" write="true" search="true" optional="false" initial="true" unique="true"/>
–> read="true"
: Can be read programmatically.
–> write="true"
: Can be written or modified programmatically.
–> search="true"
: Can be used as a search criterion (filter) in FlexibleSearch queries.
–> optional="false"
: Mandatory; must always have a value.
–> initial="true"
: Value must be provided during the item’s creation.
–> unique="true"
: Value must be unique across all Manufacturer
items.
<persistence type="property"/>
–> The attribute will be stored directly as a database column.
<persistence type="dynamic" attributeHandler="dynamicManuAddressHandler"/>
<modifiers write="false" read="true"/>
–> The attribute data is a non-persistent attribute, which means it is not stored in the database and only gets dynamic values based on existing attributes within item type. So the dynamic attribute is set property write="false"
4) Practice
Using the xml code that defined the Manufacturer item type on step 3 above to practice.
Step 1: add the XML code to the trainingcore-items.xml file
Step 2: Build to generate the item type: $ ant clean all
Step 3: add the one-to-many relationship between the Manufacturer type and Product type.
Step 4: Build the Hybris system to see the relationship will generated on each item type. $ ant all
Step 5. Start the server -> update the system
Step 6: Import sample data
Step 6: Verify the result in BO.
5) Best practice
Congratulation.! Good job. You have just finished the lesson. Thanks for your concentration. If this post is useful, please share and give me a heart up in the post.
That’s all for today. In the next post, we will learn about the Converter and Populator. Hope to see you in the next article.
Happy coding.!!! <3