Overview of items.xml file

Items.xml file contains the type definition in hybris.

We can define the new item types, extend the existing item types in the items.xml file

Items.xml file is located in the resources directory of an extension as below

items xml file in extension

Items.xml file names are prefixed with respective extension name.

Example:

In core extension, it is available as core-items.xml file.
In trainingcore extension, it is available as trainingcore-items.xml file.

The basic structure of items.xml is as below

  1. <items   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.     xsi:noNamespaceSchemaLocation="items.xsd">
  3.  
  4.     <atomictypes>
  5.     ...
  6.     </atomictypes>
  7.  
  8.     <collectiontypes>
  9.     ...
  10.     </collectiontypes>
  11.  
  12.     <enumtypes>
  13.     ...
  14.     </enumtypes>
  15.  
  16.     <maptypes>
  17.     ...
  18.     </maptypes>
  19.  
  20.     <relations>
  21.     ...
  22.     </relations>
  23.  
  24.     <itemtypes>
  25.     ...
  26.     </itemtypes>
  27. </items>
<items   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="items.xsd">
 
    <atomictypes>
    ...
    </atomictypes>
 
    <collectiontypes>
    ...
    </collectiontypes>
 
    <enumtypes>
    ...
    </enumtypes>
 
    <maptypes>
    ...
    </maptypes>
 
    <relations>
    ...
    </relations>
 
    <itemtypes>
    ...
    </itemtypes>
</items>

Items.xml file is always validated against items.xsd.

Items.xsd file has defined the order in which the elements have to be defined in items.xml file.

Hence we have to maintain the above order of elements in items.xml file.

Note:

If we do not maintain the above order in items.xml file, build will be failed.

Defining new item type

we can define new item type in items.xml file using code ,deployment and all the attributes for that item type.

  1. <itemtype code="SampleType1"
  2.     extends="GenericItem"
  3.     autocreate="true"
  4.     generate="true">
  5.  <deployment table="SampleType1" typecode="11000"/>
  6. <attributes>
  7. <attribute>
  8. ………
  9. </attribute>
  10. <attribute>
  11. ………
  12. </attribute>
  13. <attribute>
  14. ………
  15. </attribute>
  16. </attributes>
  17. </itemtype>
  18.  
  19. <itemtype code="SampleType2"
  20.     extends="SampleType1"
  21.     autocreate="true"
  22.     generate="true">
  23.  <deployment table="SampleType2" typecode="11001"/>
  24. <attributes>
  25. <attribute>
  26. ………
  27. </attribute>
  28. <attribute>
  29. ………
  30. </attribute>
  31. <attribute>
  32. ………
  33. </attribute>
  34. </attributes>
  35. </itemtype>
<itemtype code="SampleType1"
    extends="GenericItem"
    autocreate="true"
    generate="true">
 <deployment table="SampleType1" typecode="11000"/>
<attributes>
<attribute>
………
</attribute>
<attribute>
………
</attribute>
<attribute>
………
</attribute>
</attributes>
</itemtype>

<itemtype code="SampleType2"
    extends="SampleType1"
    autocreate="true"
    generate="true">
 <deployment table="SampleType2" typecode="11001"/>
<attributes>
<attribute>
………
</attribute>
<attribute>
………
</attribute>
<attribute>
………
</attribute>
</attributes>
</itemtype>

We have defined 2 item types SampleType1 and SampleType2
Where SampleType2 is extending SampleType1

Order of Item Types in items.xml


Items.xml file is evaluated in one pass unlike impex which is multi pass processed.

Because of single pass,we need to specify the item types in the order of inheritance.

More abstract types has to be defined at the beginning of the items.xml file
More concrete types has to be defined at the end of the items.xml file

If we reverse the above item type definition as below, build will fail.

  1. <itemtype code="SampleType2"
  2.     extends="SampleType1"
  3.     autocreate="true"
  4.     generate="true">
  5.  <deployment table="SampleType2" typecode="11001"/>
  6. <attributes>
  7. <attribute>
  8. ………
  9. </attribute>
  10. <attribute>
  11. ………
  12. </attribute>
  13. <attribute>
  14. ………
  15. </attribute>
  16. </attributes>
  17. </itemtype>
  18.  
  19. <itemtype code="SampleType1"
  20.     extends="GenericItem"
  21.     autocreate="true"
  22.     generate="true">
  23.  <deployment table="SampleType1" typecode="11000"/>
  24. <attributes>
  25. <attribute>
  26. ………
  27. </attribute>
  28. <attribute>
  29. ………
  30. </attribute>
  31. <attribute>
  32. ………
  33. </attribute>
  34. </attributes>
  35.  
  36. </itemtype>
<itemtype code="SampleType2"
    extends="SampleType1"
    autocreate="true"
    generate="true">
 <deployment table="SampleType2" typecode="11001"/>
<attributes>
<attribute>
………
</attribute>
<attribute>
………
</attribute>
<attribute>
………
</attribute>
</attributes>
</itemtype>

<itemtype code="SampleType1"
    extends="GenericItem"
    autocreate="true"
    generate="true">
 <deployment table="SampleType1" typecode="11000"/>
<attributes>
<attribute>
………
</attribute>
<attribute>
………
</attribute>
<attribute>
………
</attribute>
</attributes>

</itemtype>

Since SampleType2 is more concrete than SampleType1, SampleType1 has to appear before the SampleType2 in the items.xml file.


Classes generated after build


1) Generated*.java source files for all item types of an extension to the gensrc directory of your extension.

2) *Model.java model classes for all item types of an extension to the bootstrap/gensrc directory

where * has to be replaced with item type code.

for item type SampleType1, GeneratedSampleType1.java and SampleType1Model.java files are generated once the build is done.

Note:

After build,only java changes will reflect but DB changes in Hybris for the new item type will be reflected only after Initialization or Update.

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on