Understanding Dynamic Attributes in SAP Hybris

In SAP Commerce (Hybris), attributes are typically stored in the database as part of itemtypes. However, there are scenarios where you don’t want to persist data but instead calculate it dynamically at runtime. This is where Dynamic Attributes in SAP Hybris come into play.

Dynamic attributes are attributes whose values are generated at runtime through custom logic rather than being persisted in the database. They are particularly helpful when the value depends on other attributes or external factors.

What is a Dynamic Attribute?

The system computes the value of a dynamic attribute each time it is accessed, rather than persisting it. Instead of retrieving data from the database, Hybris invokes a handler class that contains the logic to generate the value.

For example:

  • Calculating the discount percentage
  • Determining whether a product is in “low stock”
  • Constructing a full name from first name and last name

Why we use Dynamic Attribute

Dynamic attributes are useful when:

  • You don’t want to store redundant data in the database
  • The value changes frequently and depends on other fields
  • you want to combine multiple internal fields into one user-friendly display string for the UI
  • You want to keep database schema clean and optimized

How Dynamic Attribute works internally

Whenever a dynamic attribute is accessed:

  1. Hybris identifies that the attribute is dynamic
  2. It calls the associated DynamicAttributeHandler
  3. The handler computes the value
  4. The system returns the value to the caller.

No database interaction happens for this attribute.

Step by step implementation with practical example

Let’s create a dynamic attribute called hasFiveG for a custom item type Smartphone, which returns true if the smartphone’s networkType is FIVEG.

  1. Define attribute in *-items.xml:
    Add the attribute to your extension’s *-items.xml with persistence type=”dynamic” and point to your handler bean via the attributeHandler element.

<itemtype code="Smartphone" extends="BaseElectronics" generate="true" autocreate="true">
	<deployment table="smartphone" typecode="12001"/>
	<attributes>
		<attribute type="java.lang.Boolean" qualifier="hasFiveG">
			<modifiers read="true" write="false"/>
			<persistence type="dynamic" attributeHandler="electronicFiveGDynamicAttributeHandler"/>
			<description>Dynamic attribute derived from networkType</description>
		</attribute>
	</attributes>
</itemtype>
  1. Create Dynamic Attribute Handler:
    Your handler must implement DynamicAttributeHandler<Value_Type, Model_Type>

public class ElectronicFiveGDynamicAttributeHandler extends AbstractDynamicAttributeHandler<Boolean, SmartphoneModel>
{

	@Override
	public Boolean get(final SmartphoneModel model)
	{
		return model != null && NetworkType.FIVEG.equals(model.getNetworkType());
	}
}
  1. Spring Bean Configuration:
    Declare your handler in your extension’s *-spring.xml configuration. The bean ID must exactly match the attributeHandler value used in the *-items.xml.

<bean id="electronicFiveGDynamicAttributeHandler" class="com.electronic.dynamicattribute.ElectronicFiveGDynamicAttributeHandler"/>
  1. Do build. Since these changes don’t affect the database, you don’t need a system update.

Once implemented, you can access it like a normal attribute.


Boolean hadFiveG = smartphoneModel.getHasFiveG();

Even though it looks like a normal getter, internally it triggers the handler.

Interviewers commonly ask about dynamic attributes when you’re preparing for SAP Hybris interviews. Make sure you understand both implementation and real-world use cases.

Leave a Reply

Your email address will not be published. Required fields are marked *