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

  1. <relation code="Country2RegionRelation" generate="true" localized="false" autocreate="true">
  2.             <sourceElement type="Country" qualifier="country" cardinality="one">
  3.                     <modifiers read="true" write="true" search="true" optional="false" unique="true"/>
  4.             </sourceElement>
  5.             <targetElement type="Region" qualifier="regions" cardinality="many">
  6.                     <modifiers read="true" write="true" search="true" partof="true"/>
  7.             </targetElement>
  8. </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

  1. private Collection<RegionModel>_regions;
private Collection<RegionModel>_regions;



RegionModel.java

  1. 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

country_table_for_relation

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

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

After_country_region_merge

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

  1. <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

  1. <itemtype code="Country" extends="C2LItem" jaloclass="de.hybris.platform.jalo.c2l.Country"
  2.                autocreate="true" generate="true">
  3.      <deployment table="Countries" typecode="34"/>
  4.     <attributes>
  5.                  <attribute autocreate="true" qualifier="regions" type="RegionCollection">
  6.                         <modifiers read="true" write="true" search="false" optional="true"/>
  7.                     <persistence type="jalo"/>
  8.             </attribute>
  9.     </attributes>
  10. </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

  1. 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

country_table_for_collection

Region Table

region_table_for_collection

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

country_holds_pks-of-region_collection

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_cons_of-collection

Pros and Cons of Relation

pros_cons_of-relation.png

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.

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