Defining collection in items.xml
- 25th Oct 2016
- 11
- 82722
- Collections in items.xml How collection of String list of string set of string collection of another item type defined in items.xml How collections are stored in DB or database How collections will be saved or stored in DB or database how to define collection of item types in items.xml in hybris How we define collections in hybris
Collection basically contains the elements of element type.
element type means the type of elements we are adding into the collection.
Example:
Collection of String will contain the elements of String element type.
Collection of Integer will contain the elements of Integer type.
Collection of Address will contain the elements of Address type.
Note:
Element type can be a simple atomic type or it can be any other complex type.
We define collection of string as below
- <collectiontype code="StringCollection" elementtype="java.lang.String" autocreate="true" generate="false"/>
<collectiontype code="StringCollection" elementtype="java.lang.String" autocreate="true" generate="false"/>
We have not specified the type,so it takes Collection of String by default.
We define List of string as below
- <collectiontype code="StringCollection" elementtype="java.lang.String" autocreate="true" generate="false" type="list"/>
<collectiontype code="StringCollection" elementtype="java.lang.String" autocreate="true" generate="false" type="list"/>
We have specified type=list so It will be List of String.
We define Set of string as below
- <collectiontype code="StringCollection" elementtype="java.lang.String" autocreate="true" generate="false" type="set"/>
<collectiontype code="StringCollection" elementtype="java.lang.String" autocreate="true" generate="false" type="set"/>
In all the above definitions, elementtype is String which is an atomic type
We define Collection of Address as below
- <collectiontype code="AddressCollection" elementtype="Address" autocreate="true" generate="false"/>
<collectiontype code="AddressCollection" elementtype="Address" autocreate="true" generate="false"/>
Here elementtype is Address which is a complex type and also another item type.
We define List of Language as below
- <collectiontype code="LanguageList" elementtype="Language" autocreate="true" generate="false" type="list"/>
<collectiontype code="LanguageList" elementtype="Language" autocreate="true" generate="false" type="list"/>
Here elementtype is Language which is a again another item type
How we use these collections with item type?
Once we have created the collection, we need to inject them to the required item type.
We can inject list of language and list of String to our CustomUser type as below
- <itemtype code="CustomUser" autocreate="true" generate="false">
- <deployment table="CustomUser" typecode="11001" />
- <attributes>
- <attribute qualifier="userId" type="java.lang.String">
- <modifiers optional="false" unique="true"/>
- <persistence type="property" />
- </attribute>
- <attribute qualifier="languages" type="LanguageList">
- <modifiers unique="false" read="false"/>
- <persistence type="property" />
- </attribute>
- <attribute qualifier="nickNames" type="StringCollection">
- <modifiers unique="false" read="false"/>
- <persistence type="property" />
- </attribute>
- </attributes>
- </itemtype>
<itemtype code="CustomUser" autocreate="true" generate="false"> <deployment table="CustomUser" typecode="11001" /> <attributes> <attribute qualifier="userId" type="java.lang.String"> <modifiers optional="false" unique="true"/> <persistence type="property" /> </attribute> <attribute qualifier="languages" type="LanguageList"> <modifiers unique="false" read="false"/> <persistence type="property" /> </attribute> <attribute qualifier="nickNames" type="StringCollection"> <modifiers unique="false" read="false"/> <persistence type="property" /> </attribute> </attributes> </itemtype>
We can see that 2nd attribute has a qualifier called “languages”
And its type is “LanguageList” which is basically the code we defined in the collection of Language using below tag
- <collectiontype code="LanguageList" elementtype="Language" autocreate="true" generate="false" type="list"/>
<collectiontype code="LanguageList" elementtype="Language" autocreate="true" generate="false" type="list"/>
We can see that 3rd attribute has a qualifier called “nickNames”
And its type is “StringCollection” which is basically the code we defined in the collection of String using below tag
- <collectiontype code="StringCollection" elementtype="java.lang.String" autocreate="true" generate="false"/>
<collectiontype code="StringCollection" elementtype="java.lang.String" autocreate="true" generate="false"/>
The code in the collectiontype tag should become the type for an attribute in the attribute definition.
After adding this change in items.xml file, do the build
CustomUserModel.java file will be generated
Check this file for the attribute called “languages” and “nickNames”
It should be generated as below
- private List<LanguageModel> _languages;
- private Collection<String> _nickNames;
private List<LanguageModel> _languages; private Collection<String> _nickNames;
So we have defined List of langauge and a collection of String for CustomUserModel.
While defining collection,
make autocreate as true so that collection type gets created in the backend.
make generate as false as we don’t need to generate Jalo class for collection type.
How collection will be stored in DB ?
There are 2 cases in Collection
1)Collection of Atomic type
In our CustomUser item type we have defined List of nickName Where nickName is a String which is atomic type.
If collection contains atomic types then the values are stored as binary fields in the database.
2)Collection of Non atomic/composed type
In our CustomUser item type we have defined List of Language Where Language is a separate item type.
If collection contains complex types then those items PKs are stored as one single String separated by comma for each PK in the database.
Hi, nice tutorial.
Can you give list of all tags that we can use within attribute tag .
Hi KB,
why collections are limited in Hybris while in JAVA these are Dynamic In nature?
Hi Shiv,
In Java , Collections are dynamic when we declare it and use it within Heap memory but In Hybris, collections are stored as comma separated values in one column of a table in database.
So because of column size restriction in DB, we have that in Hybris,
nice explanation KB!
Hi- I want to insert characters in a column which is more than 8000 in length. Which attribute type would be suitable for me? Could you provide with an example please?
As this example,I want search the CustomUser table’s all languages.
I can get all languages’s pks, then search them in the Languages table.
So there are other ways?
thanks
1) For LanguateList collection type, element type given as Language. And I believe there should be an item type defined for Language. Please correct me if I am wrong.
2) autocreate should be false as there is no need of new database entry for collection type.
Yes , Language type is defined already, its just a collection of Language.
In Database , Collection entries are stored as comma separated.
In the CustomUser table, languages attribute will hold the collection entries of type Language.
Then what is the need to have as autocreate=”true” in the collectiontype tag shown as below(In other words, any table entry will be created for collectiontype?).
Observed the same case with relations also. Please elobarate on this 🙂
in above example Generate should be false I guess.
You are right Ananda, It has to be false as for collection no Jalo class is required.
Updated it.
Thank you for noticing and pointing it out.