Collection v/s relation in Hybris
We know that Collection can be used as an alternative for OneToMany relation
But in that case, why do we go for Relation sometimes and why do we go for collection some other times?
Let’s see in detail what happens when we go for collection and what happens when we go for relation.
Requirement : Need to define data model such that One Country will have many Regions
In this kind of requirement, we have 2 options to choose in Hybris
1) Relation
2) Collection
Relation
We know that Relation can be defined as below
- <relation code="Country2RegionRelation" generate="true" localized="false" autocreate="true">
- <sourceElement type="Country" qualifier="country" cardinality="one">
- <modifiers read="true" write="true" search="true" optional="false" unique="true"/>
- </sourceElement>
- <targetElement type="Region" qualifier="regions" cardinality="many">
- <modifiers read="true" write="true" search="true" partof="true"/>
- </targetElement>
- </relation>
<relation code="Country2RegionRelation" generate="true" localized="false" autocreate="true"> <sourceElement type="Country" qualifier="country" cardinality="one"> <modifiers read="true" write="true" search="true" optional="false" unique="true"/> </sourceElement> <targetElement type="Region" qualifier="regions" cardinality="many"> <modifiers read="true" write="true" search="true" partof="true"/> </targetElement> </relation>
After adding this change and build successful, we can see the classes as below
CountryModel.java
- private Collection<RegionModel>_regions;
private Collection<RegionModel>_regions;
RegionModel.java
- private CountryModel _country;
private CountryModel _country;
we can observe that there is a Bidirectional mapping is done.
We can get collection of regions from country and we can get country from region to which the region belongs to.
Tables for this relation are as below
Country table
since there is a relation between 2 tables,The primary key of Country table will become the foreign key in Region table and looks like below
Region table
Assume that country with PK value C1 has 2 regions(region with PK value R1 and R2)
Country with PK value C2 has 3 regions(region with PK value R3,R4 and R5)
Region Table with above details
We can see that,Region table has Country’s PK has a foreign key which is associated for each row of a Region.
Collection
Let’s use Collection for the same
First we need to define the collection type as below
- <collectiontype code="RegionCollection" elementtype="Region" autocreate="true" generate="false"/>
<collectiontype code="RegionCollection" elementtype="Region" autocreate="true" generate="false"/>
Now we need to add this collection to Country item type as below
- <itemtype code="Country" extends="C2LItem" jaloclass="de.hybris.platform.jalo.c2l.Country"
- autocreate="true" generate="true">
- <deployment table="Countries" typecode="34"/>
- <attributes>
- <attribute autocreate="true" qualifier="regions" type="RegionCollection">
- <modifiers read="true" write="true" search="false" optional="true"/>
- <persistence type="jalo"/>
- </attribute>
- </attributes>
- </itemtype>
<itemtype code="Country" extends="C2LItem" jaloclass="de.hybris.platform.jalo.c2l.Country" autocreate="true" generate="true"> <deployment table="Countries" typecode="34"/> <attributes> <attribute autocreate="true" qualifier="regions" type="RegionCollection"> <modifiers read="true" write="true" search="false" optional="true"/> <persistence type="jalo"/> </attribute> </attributes> </itemtype>
After doing build we can see below changes
CountryModel.java
- Collection<RegionModel> regions;
Collection<RegionModel> regions;
But inside RegionModel.java,we will not see CountryModel reference because it’s a collection not a relation
Country Table
Region Table
There is no relation between 2 tables rather Country has to hold the collection of Region.
Hence it stores the PK of Regions on each country.
Assume that country with PK value C1 has 2 regions (region with PK value R1 and R2)
Country with PK value C2 has 3 regions (region with PK value R3,R4 and R5)
Country Table with above details
Here we don’t have bidirectional relation between Country and Region table.
Country table holds the PKs of Region table by comma separated.
Since it stores the PKs in a comma separated way, there is a chance of data truncation if there are many Regions associated for one country because each column in Database has its own characters limit.
Pros and Cons of Collection
Pros and Cons of Relation
When to use and when not use Collection/Relation?
There is no such hard and fast rule for choosing Collection/Relation, we just need to consider few points before choosing it.
When to Use Collection?
Prefer collection when we are sure that in our current and future requirements, we will not have many rows mapped for one side.
It means whenever the collection size is small,we can prefer collection as it helps to achieve faster retrieval
When not to use Collection?
Don’t use collection whenever the collection size is very big as it can lead to data truncation
When to use Relation?
Whenever the collection size is bigger or there is a chance that it can grow bigger then prefer Relation as it assures that there will be no data truncation.
For many to many , we should go for Relation always.
When not to use Relation?
We can just prefer collection in place of Relation whenever the collection size is smaller to compensate slow retrieval of Relation but in that case we need to negotiate with Bidirectional mapping.
So choose it based on the above factors which suits your requirements.
if more data available in collection, which data will be removed, i mean from starting or from last.
see here, when ever we are update values in a particular cell at that time the value is updated
for ex: a cell having three values r1,r2,r3 now you want to update a value r4 in this time r4 replaced r1,r2,r3.
In this case we can’t change the one value at run-time
Hi Karibasappa.
Please, do you know how to assign a default value to a relation that is going to be displayed as <list:column in the backoffice?
Thank you!.
Well explained. Thanks, KB.
Can you explain about Many-Many relations with example?
In Relation, assume that country with PK value C1 has 2 regions(region with PK value R1 and R2). In this case, how the region PK value will be stored in country table? Will it be a comma separated value like R1,R2?
Thanks in advance
Yes, it’s correct.
Since Relation also stores the PKs in a comma separated way, there is a chance of data truncation.
So its CONS for relation as well?
Where did you read relation also stores PK’s in comma separated way ? and its not correct wherever you read.
it never stores as comma separated in case of relation.
Please read this article once again to understand how it will be stored in relation.
Hi KB,
In the previous comment you have replied “Yes it’s correct”. So i came to a conclusion that Region’s PK value will be store in Country table as comma separated value for RELATIONS.
In this article i could not see the Country table’s diagram with Regions value for RELATIONS. Help me to understand.
No, I thought you are asking the relation between country and region as collection in mind, because if you read this article, I have categorically explained relation will not have comma separated pks but collections will have comma separated pks.
Please explain where you are facing difficulty in understanding this.
I understood this article. My one doubt is that how the Relation’s Country table will hold multiple Region values.
Please observe tables provided for relations in this article, You will get answer.
Hi KB
How do we manage one-to-one relationship in hybris, is the collection most suitable approach or the relations ?
One to One relationship can be defined in Hybris using attrtibute qualifier with its type as another item type.
Is the collection most suitable approach or the relations ?
Please read this article again especially below section
When to use and when not use Collection/Relation?
Hi KB
What will country table hold after having one to many relation with region?
Thanks in advance!
One Country can have any number of Regions, so it holds One to Many relation with Regions.
In addition to this , country will have its own details like name,isoCode,etc
Can you please tell me that is one to one relation possible. If not, why? Although I know it is useless as we can achieve this by adding attribute to a type.
It was asked in interview. So i want to know the proper answer for this.
Thanks in advance!
You can answer as below
One to One relation is possible in Hybris but syntactically not using relation tag but by adding attribute to a type.
Since We don’t need to mention relation attributes like cardinality etc for One to One as Hybris assumes it as One to One if we add another type as attribute.
But One to One relation is formed in the DB.
Hi KB,
what is the need of relations in items.xml…?
Is there any disadvantages of collectons in items.xml file..?
Hi Sandeep,
You can see the pros of Relation in this article which tells the need of relation.
You can see the cons of Collection in this article to know its disadvantages.
Hi Karibasappa G C,
Can you please explain why relations are defined before itemtypes as at the time of parsing of items.xml relations are created before the itemtypes defined in the relation. How does that work?
Thanks in advance.
Hi Vidit,
The ordering of elements in items.xml is completely controlled and defined by items.xsd schema file.
As every XML file has to follow the rules of schema definition file which basically defines the syntax of xml file , items.xml also has to follow the rules of items.xsd.
I don’t think they have defined in the order of processing because we need item types to be created to maintain the relation and also we need item types to be created to maintain the collection.
Hi Karibasappa G C,
can you tell how to read impex
Hi Ankit,
I will post on it whenever i get time.
You can read it from WIKI and go through some impex examples!!