Defining an Item types

As we know, the *-Items.xml file is used to create new tables. We can either create our own custom item type or extend it to create a new item type with some additional fields. GenericItem is the default parent of each item type.

Note : This is recommended before creating an item type. Please read about some terminology that will be used inside the item definition. If you already know, then it's good; otherwise, Please click here to learn about the same.

Basically there are three way to define new item type.

  1. Create new item type without extending any existing item type / By extending GenericItem item type.
  2. Create new item type by extending it with existing item type.
  3. Re-Define the existing item type again with new attributes.

1. Create new item type without extending any existing item type / By extending GenericItem item type :

<itemtype code="CSCodeInfo"
                  jaloclass="de.hybris.platform.jalo.cors.CSCodeInfo"
                  autocreate="true"
                  generate="true">
            <deployment table="CSCodeInfo" typecode="18000"/>
            <attributes>
                <attribute qualifier="codeId" type="java.lang.String">
                    <persistence type="property"/>
                    <modifiers optional="false" initial="true" read="true" write="false" unique="true" />
                </attribute>
                <attribute qualifier="name" type="java.lang.String">
                    <persistence type="property"/>
                    <modifiers read="true" write="true"/>
                </attribute>
                <attribute qualifier="value" type="java.lang.String">
                    <persistence type="property"/>
                    <modifiers optional="false" initial="false" write="true"/>
                </attribute>
            </attributes>
            <indexes>
                <index name="codeIdKey" unique="true">
                    <key attribute="codeId"/>
                </index>
            </indexes>
        </itemtype>

Note : For this option, we should use "GenericItem" as a super class (extends="GenericItem"). If we are not defining this, then by default, Generic Item will be the parent of each item type.

A concise explanation of the used terminology tag wise:

itemtype tag : 

code : This is a unique identifier for each record.

autocreate : This indicates that the hybrid system should create a new database entry for this at the time of initialization or update. If we set it to false, the build will fail. For the first creation, this should be true.

generate : This indicates the hybrid system generate a new jalo class for this type during the build. If we set it to false, then the jalo class will not be generated, but the model class will always be generated. We need to set this true for the first creation of the item type.

jaloclass : We use to define fully qualified classpath of this ItemType. 
deployment tag : 

table : In this section we provide the table name where the instance will store.

typecode : As a typecode we specify the unique number that will be used internally by Hybris as table reference (we must use an integer value between 10000 and 32767 as per hybris standard recommendation).
Sub tag under attributes tag :

attribute tag : 

qualifier : Used to provide identifier of the Attribute.
type : We use this to provide identifier of the type this for which attribute is going to be created.

modifiers tag :

optional : This contains a boolean value as true/false.Default value is true (here true means attribute is mandatory and will act opposite for false).

search : This contains a boolean value as true/false.Default value is true (here true means attribute can be searchable by a FlexibleSearch and will act opposite for false).

read : If we set modifier to true results in a getter method being generated for this attribute and while we set modifier to false results in no getter method being generated for the attribute.

write : If we set modifier to true results in a setter method being generated for this attribute and while we set modifier to false results in no setter method being generated for the attribute.

unique : if we set as true, then it's mandatory that attribute must hold a unique value.

persistence tag : 
there are two value that can be set for persistance type that is dynamic or property.

dynamic : This indicates hybris not to create column in DB because this attributewill not be save. For this attribute, value will be persist at run time after calculation from there defined attribute handler so this will be called daynamic attribute.  
Example : (<persistence type="dynamic" attributeHandler="offerCartTotalAttributeHandler"/>)

Please click here to know more about dynamic attribute

property : This indicates hybris that create column in DB and the value will be store. 

2. Create new item type by extending it with existing item type :

<itemtype code="CSProduct" extends="Product"
				autocreate="true" generate="true" jaloclass="org.cs.core.jalo.CSProduct">
				<description>CS product extension that contains additional attributes.</description>
				<attributes>
					<attribute qualifier="genders" type="GenderList">
						<description>List of genders that the CSProduct is designed for</description>
						<modifiers />
						<persistence type="property" />
					</attribute>
				</attributes>
			</itemtype>

Note :  In this case of creating new item type we use extends keyword for extend the super class and keep autocreate and generate set as true because we are defining new type.

3. Re-Define the existing item type again with new attributes :

<itemtype code="B2BUnit" autocreate="false" generate="false">
			<attributes>
				<attribute qualifier="approvalProcessCode" type="java.lang.String">
					<modifiers read="true" write="true" search="true"
						optional="true" />
					<description>A name of the process to be consumed by process engine for
						b2b order approval as defined in the process definition file.</description>
					<persistence type="property" />
				</attribute>
			</attributes>
		</itemtype>

Note : During development, sometimes we get the requirement to update the existing item type with some new attribute, and in this case, we handle it as per the 3rd option of item re-definition in our respective extension. To achieve this, we need to copy the basic sculton on the existing itemtype in the respective custom extension and add the require custom attribute under the attribute tag. We also need to keep false autocreate and generate. 
After build we can see that newly created attribute in the existing data model instead of creating a new data model.

Leave a comment