A “relation” is like a way to show how things are connected or associated with each other. In some cases like less data store in table as collection then Collection can be used as an alternative for OneToMany relation.
Whene we use relation then a bidirectional mapping get created.By this feature complete information of source element can be fetch by target element and vice versa.
In one to many/ many to one relation get maintain using in target table by concept of foreign key.Where target model PK has a foreign key which is associated for each row of a source model.
In many to many relation a new table get created hence each value will be stored in a separate table row. Because of this separate column maintenance feature, we will never get the data truncation issue. But while we fetch the data, we need to use a join-in query, which can cause a slow retrieval of data.
<relation code="MediaContainer2MediaRel" localized="false" generate="true" autocreate="true"> <sourceElement type="MediaContainer" qualifier="mediaContainer" cardinality="one"> <modifiers read="true" write="true" search="true" optional="true"/> </sourceElement> <targetElement type="Media" qualifier="medias" cardinality="many"> <modifiers read="true" write="true" search="true" optional="true"/> </targetElement> </relation>
Collection :
When we use collection to store the data then for which model attribute we define only in that model we cal store the data.There will be no any interconnection or link will be create in that item type and collection elementtype.
Step 1 . Define collectiontype : <collectiontype code="navigationBarComponentList" elementtype="NavigationBarComponent" autocreate="true" generate="true" type="list" /> Injection of created collection in item type : <itemtype code="NavigationBarCollectionComponent" autocreate="true" generate="true" extends="SimpleCMSComponent" jaloclass="de.hybris.platform.acceleratorcms.jalo.components.NavigationBarCollectionComponent"> <description>Deprecated since 6.2, please use NavigationComponent instead. It represents account navigation bar component that contains cms navigation node.</description> <attributes> <attribute qualifier="components" type="navigationBarComponentList"> <persistence type="property" /> <modifiers /> <description>A collection of navigation bar components</description> </attribute> </attributes> </itemtype>
As in the above example, there will be no interlink created between NavigationBarComponentModel and NavigationBarCollectionComponentModel. Because of this, we don’t have bidirectional relations in these models. In NavigationBarCollectionComponentModel, a new attribute will be created, and in that attribute, column PK of all NavigationBarComponentModels will be stored as comma-separated. So fetching the data is very fast. As per the future perspective, if collection size continues to grow, there’s a possibility of data loss (data truncation).
Conclusion: The selection of any one is based on the project requirements, but we need to know a few pros and cons before we can make a better decision.
Colletion and relation are both good to use, but we need to keep in mind the behavior information of both. If we are mostly confirmed and there is no more data to store in collection and we also need not to mostly use the bidirection retrival feature in the application, then we can go with collection because retrieval of data is fast. But whenever the size of the collection is very large or there is a lot of chance that the amount of data will grow in the future, we always prefer Relation because here we can escape the data truncation issue.