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
- <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>
<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.
- <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>
<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
- <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>
<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.
It would have been great if we could elaborate more on modifiers. Eg. ‘initial’ modifier is one of the most import modifiers which is generally asked in interviews.
autocreate =false and generate=false , in this scenario, in the existing item type attributes are getting created and even columns are getting created in the table but attributes are not added to existing model class. Can you please explain me the reason?
Make generate true, to get attributes in the model class
next generation cockpit dynamic forms can be used to
Thanks a lot!!
For the 3rd case:
We add a new attribute for the type. However, we set “generate = false”, which means that the jalo class will not be generated, hence it will not contain the newly-added attribute
Am I correct?
What does “abstract=true” mean on the item level?
Can it have a deployment table?
hi,
please include how to implement load, prepare and other interceptors in data modelling
Hi,, i’m new to hybris. I wanted to know what is a jalo class?
Thank You KB for the precise explanation.I have a query here.
How is it different from property/dynamic?
Hi KB,
Nice explanation.
In point no 3, Define the existing item type again with new attributes
PermanentAddress
Boolean.FALSE
What if we make autocreate=”true” and generate=”true” at item type level?
Will the build fail?
yes and it will show error as : no deployment specified for non abstract type Address
Build will success but out of the box code will gone
Hi KB,
I am new to hybris, want to understand 2nd case(Define new item type but extend it with existing item type).
Is there any possibility without using Jalo class we can achieve this.
Thanks in advance,
Vivek Gupta
Hi KB,
Base apparel product extension that contains additional attributes.
List of genders that the ApparelProduct is designed for
Define new item type but extend it with existing item type,In this case If I am not wrong when we
are mentioning “generate=true” that means jalo class is generating. ApparelProduct
jalo class will be created.then what is meaning of this below “jaloclass” attribute and why we are
using jaloclass=”org.training.core.jalo.ApparelProduct”
please clear my doubt
Hi,
generate=true at the item type level hints hybris to generate a new jalo class for this type during build time.
using jaloclass=”org.training.core.jalo.ApparelProduct” will provide us the flexibility to specify the custom name for the jalo class.
what is default name for the jalo class, if we not specify jaloclass=”org.training.core.jalo.ApparelProduct” ?
hi bro…..
in my laptop config is quad core 4 gb ram acer 5560 hybris 5.4 version build sucess ….but when i start server i get “”filestr ” internal external not recognised .. error whats prob. bro pls help
Hi,
Try to do any one of the below option ,then restart the system and check it
1) Copy findstr.exe from c:/windows/system32 and paste it in C:\Program Files\Java\jdk\bin
2) Add C:\Windows\System32 to your PATH environment variable
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….
My Question is..
At the time of writing new item type (i.e this case:Define new item type without extending any existing item type) with generate=”false”…i am getting both jalo and model classes will not be generated…..but you said that If we set it to false,then jalo class will not be generated however model class will always be generated….
Am i correct or not…please suggest me
Ya you are right and its expected behavior.
My point is Model class creation never depends on generate value, irrespective of generate true or false model class will be generated.
But if its a new item type, then hybris makes it mandatory for us to define generate as true so that it creates a Jalo class for it(It can be used in some cases).
Thanks know for cleaning my doubt…
Hi KB,
Superb and simple explanations through out the blog. Here i have a question
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.
when you say “new database entry” is it definition entry in composite type table to store definition ?
Hi Sateesh,
Thank you!!
Yes you are right, Hybris stores all types to refer them when we make its reference in some places like Flexible search query.
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.
Question is , What is the basic use of JALO class, and its behavior in hybris ?
Jalo class in hybris is used to write some business logic related to promotions/vouchers.
Its been depreciated but in promotion/vouchers logic we still need to depend on Jalo.