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.
The items.xml file specifies the types of an extension. 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.
<itemtype
code="Publication"
jaloclass="de.hybris.platform.print.jalo.Publication"
extends="GenericItem"
generate="true"
autocreate="true">
<deployment table="Publication" typecode="23402"/>
<attributes>
<attribute qualifier="code" type="java.lang.String">
<modifiers optional="false" />
<persistence type="property"/>
</attribute>
<attribute qualifier="sourceCatalogVersion" type="CatalogVersion">
<modifiers write="true" search="true" search="true" optional="true" />
<persistence type="property" attributeHandler="beanId" />
</attribute>
<attribute qualifier="rootChapters" type="ChapterCollection">
<modifiers write="false" search="false"/>
<persistence type="dynamic" attributeHandler="beanId" />
</attribute>
</attributes>
</itemtype>
Let’s discover each of their meaning.
code="Publication"
–> The name of class, and maybe the name of the table in the database as well.
autocreate="true" generate="true"
–> This mean just creating the system data object during initialization and creating the Jalo class
<deployment table="Publication" typecode="23402"/>
–> 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 hybris system, the remaining for the custom type.
<persistence type="property"/>
–> The attribute will be stored in a database.
<persistence type="dynamic" attributeHandler="beanId"/>
–> The attribute data will not persist in the database, only stored in the memory of the server (RAM)
–> With the attributeHandler, we can define a spring bean in the .xml file to handle the value for this attribute. In this handler class, we implement the class DynamicAttributeHandler<Target, Source>, to return the target value.
<persistence type="property" attributeHandler="beanId"/>
–> With the attributeHandler we can define a spring bean in the .xml file to handle the value for this attribute. In this handler class, we implement the class AttributeHandler<Target, Source>, to modify the value before persisting in the database.
<modifiers read="true" write="true" search="true" optional="true"/>
–> Getter or setter will be generated if read and write are true.
–> Search is true mean allow to search by flexible search
–> Optional is true means this field is not required.
4) 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