Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between relations and collections in hybris?

I am new to hybris, What is diff b/w relations and collections, why we go for relations instead of collections.

like image 551
P C raja Avatar asked Jan 30 '15 06:01

P C raja


People also ask

What is the difference between collection and relation in hybris?

Collection Type: Collection Types are comma separated Pks that gets stored in the database. For example, when we need to store the keywords applicable for the product we can use collection type. Relation: Relation is used when we need to model the association between objects.

What is relation in hybris?

To create dependency between 2 types is called relation between the types. Relation is basically used between 2 itemtypes in hybris So each of them can also access the instance of its partner itemtype. It's a relation between 2 table in the terms of Database.

How are collections stored in hybris?

"How the data is stored in both of them" In collections, a new column is created in table(item), containing comma separated primary keys of the list elements. The actual list elements are stored in another table. In relations, a new table is created as a link table between two item types.


4 Answers

Basically, there are two technically different ways of modeling collections in hybris:

  1. CollectionTypes

    • Think of CollectionTypes in hybris as a backpack mounted onto a type
    • By runtime, CollectionTypes are resolved into a Collection of a kind of item, such as a List of MediaModels
    • Can cause overflow, resulting in truncation and therefore loss of data
    • More difficult to search and lower performance
    • On the database level, CollectionTypes are a comma-separated list of PKs, so there is a maximum
  2. RelationTypes

    • Create links between all kinds of types Create type-safe n-to-m relations: Only link such elements of the source / target type declared at the relation
    • Values for relations are stored in a separate database table +Each value is stored in a separate table row
like image 111
Mafick Avatar answered Nov 08 '22 23:11

Mafick


I'm totally agree with @KilleKat comment, he has mentioned all the differences between CollectionType and RelationType in Hybris.

I attached bellow some diagrams to have a more clearer view about the subject.

CollectionTypes: (to be used wisely) enter image description here

RelationTypes: (recommended) enter image description here

like image 32
Mouad EL Fakir Avatar answered Nov 09 '22 00:11

Mouad EL Fakir


As Sumit says above,

CollectionType is discouraged and RelationType should be used whenever possible. This is because, the maximum length of the database field of a CollectionType is limited and a CollectionType with many values may end up getting its values truncated. In addition, the values of CollectionTypes are written in a CSV format and not in a normalized way. By consequence, hybris recommends using RelationTypes whenever possible.

  • CollectionType: CollectionTypes are based on the Java Collection class i.e. a Collection is a list of elements.
    1:n - Keep links to the respective values via an attribute on the source item, for example, a list of Primary Keys.
    n:1 - Store the attribute values at the respective target items and have a getter method at the source type to retrieve the values.
  • RelationType:
    n:m - Internally, the elements on both sides of the relation are linked together via instances of a helper type called LinkItem. LinkItems hold two attributes, SourceItem and TargetItem, that hold references to the respective item.

For each entry within a relation (in other words, for each link from one item to another), there is a LinkItem instance that stores the PKs of the related items. LinkItem instances are handled transparently and automatically by the platform: On the API level, you only need to use the respective getter and setter methods.

like image 40
super.rach Avatar answered Nov 09 '22 00:11

super.rach


Its important to understand hybris strongly discourages using collections, use relations instead.

As stated above collections are maintained as comma separated from a data structure prospective and thats why you might see problem of data truncate, where as relations have rational data structure of creating a new table and map table to join the two table.

Collection because of there storage structure - can't be searched.

I would say for a very simple (1:n) relationship with limited data - you can still use collections. While for any complex (m:n /1:n) relationship always use relations

like image 26
Sumit Juyal Avatar answered Nov 09 '22 00:11

Sumit Juyal