Defining Item types in hybris

There are 3 ways of defining an item type in Hybris.

We need to decide one of the ways based on the requirement.

What are those 3 ways?

1) Define the new item type without extending any existing item type

2) Define the new item type by extending it with existing item type

3) Define the existing item type again with new attributes

1) Define new item type without extending any existing item type


In this case, we are supposed to define the new item type in our items.xml file without extending any of the existing item type.

Requirement:
Store third party integration system credentials like username and password in DB and access them while making a third party call.

As per the requirement,we need to define one new item type which can store username and password.

So create a new item type with attributes as below

  1. <itemtype code="IntegrationSystemCredentials" autocreate="true" generate="true">
  2.                 <deployment table="IntegrationSystemCredentials" typecode="11000" />
  3.                 <attributes>
  4.                     <attribute qualifier="code" type="java.lang.String">
  5.                         <modifiers optional="false" unique="true"/>
  6.                         <persistence type="property" />
  7.                     </attribute>
  8.                     <attribute qualifier="username" type="java.lang.String">
  9.                         <modifiers unique="false"/>
  10.                         <persistence type="property" />
  11.                     </attribute>
  12.                     <attribute qualifier="password" type="java.lang.String" >
  13.                         <modifiers unique="false" encrypted="true"/>
  14.                         <persistence type="property" />
  15.                     </attribute>
  16.                 </attributes>
  17.             </itemtype>
<itemtype code="IntegrationSystemCredentials" autocreate="true" generate="true">
				<deployment table="IntegrationSystemCredentials" typecode="11000" />
				<attributes>
					<attribute qualifier="code" type="java.lang.String">
						<modifiers optional="false" unique="true"/>
						<persistence type="property" />
					</attribute>
					<attribute qualifier="username" type="java.lang.String">
						<modifiers unique="false"/>
						<persistence type="property" />
					</attribute>
					<attribute qualifier="password" type="java.lang.String" >
						<modifiers unique="false" encrypted="true"/>
						<persistence type="property" />
					</attribute>
				</attributes>
			</itemtype>



We have defined a new item type called IntegrationSystemCredentials which has 3 attributes as explained below

Code: It will be unique identifier for each record
Username: username of the third party system
Password: password of the third party system

autocreate=true at the item type level

It hints hybris to create a new database entry for this type at initialization/update process
If we set it to false,build will fail.
We should set it to true for the first definition of item type.

generate=true at the item type level

It hints hybris to generate a new jalo class for this type during build time.
If we set it to false,then jalo class will not be generated however model class will always be generated.
We should set it to true for the first definition of item type.

Deployment table and typecode

Deployment specifies the table name where all the instances of the item type are stored.

typecode specifies the unique number which will be used internally by Hybris to reference the type.
Its value should be between 0 & 32767 and should be unique.
If we use the typecode which is already exists then build will fail.
typecode values between 0 and 10000 are reserved for hybris internal use, typecode values greater than 10000 can be used.

Attributes of an item type

We can define as many attributes as we want for an item type.
Each attribute has to specify the data type,modifiers and persistent type.
Type can be either primitive type or can be reference to any existing type.

Modifiers for attributes

read = true means attribute is readable and corresponding getter method will be generated for the attribute
Default value is true.

write=true means attribute is writable and corresponding setter method will be generated for the attribute
Default value is true.

search=true means attribute can be searchable by a FlexibleSearch
Default value is true.

Optional=true means attribute is not mandatory
Default value is true.
To make any attribute as mandatory, set optional as false

persistent type for attributes

Persistent type can be either property or dynamic.
If it is set as property then value will be stored in DB and it’s called persistent attribute.
If it is set as dynamic then value will not be stored in DB and it’s called dynamic attribute.


2) Define new item type but extend it with existing item type


In some cases we need to define new item type but we have to extend the existing item type.

Requirement:
Add a new functionality such that any product in our system should hold the list of genders and that product is used only for those genders list.
If product “A” is designed for only Male, the Product A gender list will have only Male element in it.
If product “B” is designed for both Male and Female then product “B” gender list will have 2 elements for Male and Female.

As per the requirement, we have to make sure that Product item type will have list of genders as a new element.

So let’s define one new item type called “ApparelProduct” which will extend existing item type called Product and add the new property as gender list.

  1. <itemtype code="ApparelProduct" extends="Product"
  2.                 autocreate="true" generate="true" jaloclass="org.training.core.jalo.ApparelProduct">
  3.                 <description>Base apparel product extension that contains additional attributes.</description>
  4.                 <attributes>
  5.                     <attribute qualifier="genders" type="GenderList">
  6.                         <description>List of genders that the ApparelProduct is designed for</description>
  7.                         <modifiers />
  8.                         <persistence type="property" />
  9.                     </attribute>
  10.                 </attributes>
  11.             </itemtype>
<itemtype code="ApparelProduct" extends="Product"
				autocreate="true" generate="true" jaloclass="org.training.core.jalo.ApparelProduct">
				<description>Base apparel product extension that contains additional attributes.</description>
				<attributes>
					<attribute qualifier="genders" type="GenderList">
						<description>List of genders that the ApparelProduct is designed for</description>
						<modifiers />
						<persistence type="property" />
					</attribute>
				</attributes>
			</itemtype>

We can see that autocreate and generate set as true as it’s a new item type.

3) Define the existing item type again with new attributes


In some cases we need to add attributes to the existing item type without defining a new item type.
In this case we just need to define the item type with existing item type code and add new attributes.

Requirement:
Add a new flag to the Address type to identify it as permanent address or not.

We need to add a new Boolean flag for Address type.

let’s define the Address type with existing item code as below

  1. <itemtype code="Address" autocreate="false" generate="false">
  2.             <attributes>
  3. <attribute qualifier="permanentAddress" type="java.lang.Boolean">
  4.                     <description>PermanentAddress</description>
  5.                     <defaultvalue>Boolean.FALSE</defaultvalue>
  6.                     <modifiers read="true" write="true" search="true" optional="false"/>
  7.                     <persistence type="property"/>
  8.                 </attribute>
  9.             </attributes>
<itemtype code="Address" autocreate="false" generate="false">
			<attributes>
<attribute qualifier="permanentAddress" type="java.lang.Boolean">
					<description>PermanentAddress</description>
					<defaultvalue>Boolean.FALSE</defaultvalue>
					<modifiers read="true" write="true" search="true" optional="false"/>
					<persistence type="property"/>
				</attribute>
			</attributes>

We can see that item type with code as Address is defined already in core-items.xml file and we have defined it again with same code.
So its not a new item type and hence autocreate and generate is set as false.

It means we are adding the new attribute directly in the existing item type without creating a new item type.

So newly added attribute will be generated in existing Address model class.
Because of which, we have to give autocreate and generate as false.

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