How to Create a Dynamic Attribute in SAP Hybris

Dynamic attributes in Hybris (SAP Commerce) allow you to add flexibility to your models without having to modify the database schema. This guide will walk you through the complete cycle of creating and using dynamic attributes.

What is a Dynamic Attribute?

A dynamic attribute is a calculated property on a model that isn’t stored in the database but is computed at runtime. These attributes are useful when you need to provide values based on business logic without altering the model structure.

The Full Cycle of Creating a Dynamic Attribute in Hybris

Step 1: Create the Business Logic with a DynamicAttributeHandler

You need to write the logic that will be used to compute the dynamic attribute’s value. You can achieve this by implementing a DynamicAttributeHandler class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class DiscountedPriceAttributeHandler implements DynamicAttributeHandler<Double, ProductModel> {

@Override
public Double get(final ProductModel product) {
// Business logic to calculate the dynamic attribute
double price = product.getPrice();
double discount = 0.1; // 10% discount
return price * (1 - discount);
}

@Override
public void set(final ProductModel product, final Double discountedPrice) {
throw new UnsupportedOperationException("Cannot set discountedPrice directly");
}
}

Step 2: Register the Handler in Spring Configuration

You must register the DiscountedPriceAttributeHandler in your Spring XML configuration file. This step connects the handler to your model’s dynamic attribute.

1
<bean id="discountedPriceHandler" class="com.mycompany.core.handler.DiscountedPriceAttributeHandler" />

Step 3: Define the New Attribute

You need to define the model in which you want to introduce the dynamic attribute. For this example, let’s add a dynamic attribute to ProductModel.

1
2
3
4
5
6
7
8
9
10
11
12
<itemtype code="Product" autocreate="false" generate="false">
<attributes>
<!-- Standard attributes -->
<attribute qualifier="code" type="java.lang.String" />

<!-- Dynamic attribute example -->
<attribute qualifier="discountedPrice" type="java.lang.Double">
<persistence type="dynamic" attributeHandler="discountedPriceHandler"/>
<modifiers write="false" />
</attribute>
</attributes>
</itemtype>

Step 4: Rebuild the Project

Once you’ve updated your files, rebuild the project to apply your changes:

1
ant clean all

This will compile the code, generate the dynamic model classes, and deploy them into your Hybris environment.

Step 5: Access the Dynamic Attribute in Java

You can now access the dynamic attribute in your business logic. Simply use the getter for the dynamic attribute, as if it were a standard model attribute:

1
2
3
ProductModel product = productService.getProductForCode("someProductCode");
Double discountedPrice = product.getDiscountedPrice();
LOG.info("Discounted Price: " + discountedPrice);

Step 6: Testing the Dynamic Attribute

Test the dynamic attribute by either writing unit tests or using the Hybris Backoffice to check if the dynamic attribute is computed as expected.

Key Considerations

  • Performance: Dynamic attributes are computed at runtime, so be cautious about adding complex logic.
  • Immutability: In most cases, dynamic attributes should be read-only to avoid unnecessary complexity.
  • Testing: Since the logic is part of the attribute handler, make sure to write unit tests to cover all business scenarios.

Final Thoughts

Creating a dynamic attribute in Hybris can add flexibility and power to your models, allowing you to compute values on the fly without modifying your database. This approach is particularly useful when dealing with calculated data that is not frequently used or changes often.

By following this full cycle from point A to Z, you’ll have a solid understanding of how to implement and work with dynamic attributes in Hybris.

Happy Coding!