Hybris Dynamic Attribute

In the hybris when we deal with the item attribute, to save that attribute value, we use a persistent type tag. We found two options for type : property and dynamic.

When we use persistent type as a property, Hybris will create a separate column in DB to store its value. But when we use the persistent type as dynamic, then the after build attribute will be part of the model, but hybrids will not create column and will not store them in the database. Now when we fetch the model from DB, during the filling of the model attribute, the value for this attribute is assigned after some calculation through a class that will be called attributeHandler.

We can refer to the OOTB Dynamic Attribute example:

To use dynamic attribute functionality please follow below steps :

1. Define the required attribute in your new item, and under the persistance tag, choose type=”dynamic” OR for any model existing in OOTB, copy that item’s definition, and add your attribute. Also, mark autocreate and genrate as “false” because we are going to add some new attribute only, not create a new item type.

Note: At this stage, please do not define attributehandler in the persistance tag because first we need to create attributehandler. So just add your attribute and make its persistance type dynamic, and then build your system to find that attribute in model calss. After the build, in the next step, we will create an attributehandler class for the attribute.

2. Now in you respective package create a new class by implementing DynamicAttributeHandler or by extending AbstractDynamicAttributeHandler and return the calculated value in there getter base on your requirment.

3. Define the bean entry for the attribute hander class with there bean id in that respective extension *-spring.xml file.

4. Now defined bean id of attribute handaler will be use as attributehandler in persistance tag of that item type :

Note: If we do not define a handler for that dynamic attribute, then automatically, a bean id will be generated by hybris as per standard, like ItemtypeCode_attributeQualifierAttributeHandler. We can find and use the same bean id  but if we want some customized result then we need to create a handler for the same .

Leave a comment