Defining Relation in items.xml in Hybris

Relation types help us to maintain the m:n relation between 2 tables.

Let’s consider 2 possible cases in relation

Case 1:

One to Many/Many to One
One user can have any number of contacts

  1. <relation code="User2ContactInfos" generate="true" localized="false" autocreate="true">
  2.             <sourceElement type="User" cardinality="one" qualifier="user">
  3.                 <modifiers read="true" write="true" search="true" optional="false"/>
  4.             </sourceElement>
  5.             <targetElement type="AbstractContactInfo" cardinality="many" qualifier="contactInfos" ordered="true">
  6.                 <modifiers read="true" write="true" search="true" optional="true" partof="true"/>
  7.             </targetElement>
  8.         </relation>
<relation code="User2ContactInfos" generate="true" localized="false" autocreate="true">
            <sourceElement type="User" cardinality="one" qualifier="user">
                <modifiers read="true" write="true" search="true" optional="false"/>
            <targetElement type="AbstractContactInfo" cardinality="many" qualifier="contactInfos" ordered="true">
                <modifiers read="true" write="true" search="true" optional="true" partof="true"/>

We can see below entries in model class after build

  1. private Collection<AbstractContactInfoModel> _contactInfos;
private Collection<AbstractContactInfoModel> _contactInfos;

  1. private UserModel _user;
private UserModel _user;

Since its a One To Many relation, we can observe that Collection is generated in UserModel class and just single User entity is generated in AbstractContactInfoModel class

Case 2:

Many to many
Consider the relation between Product and categories
One product can belong to many categories
And one category can contain many products.
It is m:n relation.

We can define the relation for such case as below

  1. <relation code="CategoryProductRelation" autocreate="true" generate="true" localized="false">
  2.             <deployment table="Cat2ProdRel" typecode="143"/>
  3.             <sourceElement qualifier="supercategories" type="Category" cardinality="many" ordered="false">
  4.                 <description>Super Categories</description>
  5.                 <modifiers read="true" write="true" search="true" optional="true"/>
  6.             </sourceElement>
  7.             <targetElement qualifier="products" type="Product" cardinality="many" collectiontype="list" ordered="true">
  8.                 <description>Products</description>
  9.                 <modifiers read="true" write="true" search="true" optional="true"/>
  10.             </targetElement>       
  11.         </relation>
<relation code="CategoryProductRelation" autocreate="true" generate="true" localized="false">
			<deployment table="Cat2ProdRel" typecode="143"/>
    		<sourceElement qualifier="supercategories" type="Category" cardinality="many" ordered="false">
    		    <description>Super Categories</description>
    			<modifiers read="true" write="true" search="true" optional="true"/>
    		<targetElement qualifier="products" type="Product" cardinality="many" collectiontype="list" ordered="true">
    			<modifiers read="true" write="true" search="true" optional="true"/>

We have defined the relation with code “CategoryProductRelation”
Speicifed the deployment table called “Cat2ProdRel”

We have 2 important things here,sourceElement and targetElement.
We can see that cardinality is mentioned as many in source and many in target, so it’s a many to many relation.

We can see qualifier as superCategories in the source which is of type Category.
We can see qualifier as products in the target which is of type Product.
It means Inside a Category it creates a variable called products and inside a Product it creates a variable called superCategories

Generate will have no effect for relation.

Autocreate true makes the relation type to be created.

We have specified collection type as List for target.

After the build, we can see below entries in java class

  1. private List<ProductModel> _products;
private List<ProductModel> _products;

  1. private Collection<CategoryModel> _supercategories;
private Collection<CategoryModel> _supercategories;

How Relation works in backend?

M:N relation

One table will be created for Products
One table will be created for Categories
One extra table will be created for LinkItem(elements on both sides of the relation are linked together via instances of a LinkItem table)

Each LinkItem instance stores the PKs of the related items for each row.

So every row of product with associated category will have one row in the LinkItem table with PK of Catrgory and associated PK of Product.

LinkItem instances are used internally by hybris.
We just need to call getters and setters at the API level to set and get the values.


Extra table will be created only for many to many relations

About the Author

Founder of
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

Connect with me on Facebook for more updates

Share this article on