top of page

Dynamic Attribute

​

You can add properties to your model and develop custom logic for them using dynamic attributes without changing the model class itself. They offer a way to create fresh data and access it without using a different service. Dynamic attributes are temporary pieces of information that are not stored in the database.

In this scenario, no column will be created in the database, and as a result, no values will be saved there. It is referred to as a dynamic or non-persistent characteristic.

​

Key features of Dynamic Attribute :

​

  1. The persistence type is set to dynamic persistent type=”dynamic”. 

  2. The attributeHandler points to a bean that must handle the DynamicAttributeHandler interface.

  3. One item type can have any number of dynamic attributes.

​

Practical Use Case: Show a new variable a cart age by taking the difference of  the cart creation date and current date on the cart page. 

​

We will fulfill the above requirement in following three steps:

 

Step 1: Define a new attribute cartAge in the cart model in item.xml and make persistent type=”dynamic”.

​

  1. <itemtype code="Cart" autocreate="false" generate="false">         

  2.    <description>Extending cart type with additional attributes.</description>   

  3.     <attributes>            

  4.  <attribute autocreate="true" qualifier="cartAge" type="java.lang.Integer">

  5.  <modifiers read=”true” write="true"/>                           

  6.  <persistence type="dynamic"/>         

  7.   <description>Dynamic attribute for cart age</description>        

  8.   </attribute>     

  9.   </attributes>

  10. </itemtype>

​

Step 2Define the Attributehandler which should be the bean id of the class which implements DynamicAttributeHandler.

​

  1. <itemtype code="Cart" autocreate="false" generate="false">         

  2.    <description>Extending cart type with additional attributes.</description>   

  3.     <attributes>            

  4.  <attribute autocreate="true" qualifier="cartAge" type="java.lang.Integer">

  5.  <modifiers read=”true” write="true"/>                           

  6.  <persistence type="dynamic" attributeHandler="cartAgeAttributeHandler"/>         

  7.   <description>Dynamic attribute for cart age</description>        

  8.   </attribute>     

  9.   </attributes>

  10. </itemtype>

​

Step 3: Define the custom attribute handler class: We will define the attribute handler class by extending the  AbstractDynamicAttributeHandler or by implementing DynamicAttributeHandler. 

​

  1. package com.training.attributehandlers;

  2. import de.hybris.platform.servicelayer.model.attribute.AbstractDynamicAttributeHandler;

  3. import java.time.Duration;

  4. import java.time.ZoneId; import

  5. java.time.ZonedDateTime;

  6. import org.springframework.stereotype.Component;

  7. import concerttours.model.CartModel;

  8. @Component

  9. public class CartAgeAttributeHandler extends AbstractDynamicAttributeHandler<Long, CartModel>

  10. {

  11. @Override public Long get(final CartModel model)

  12. {

  13. if (model.getCreationDate() == null)

  14. { return null; }

  15. final ZonedDateTime cartCreationDate = model.getCreationDate().toInstant().atZone(ZoneId.systemDefault());

  16. final ZonedDateTime now = ZonedDateTime.now();

  17. final Duration duration = Duration.between(now, cartCreationDate);

  18. return Long.valueOf(duration.toDays());

  19. }

  20. }

​

​

Step 4: Associate the CartAgeAttributeHandler class to the bean id defined in item.xml file (attributeHandler="cartAgeAttributeHandler"). 

​

<bean id="cartAgeAttributeHandler" class="com.training.attributehandlers.CartAgeAttributeHandler"/>

​

​

About the Author

 

Piyush Singh is a seasoned technology enthusiast and educator with a passion for making complex concepts accessible to everyone. With over 10 years of experience in the tech industry working as consultant,  Piyush specializes in Java, Sap Hybris technology and has a knack for breaking down intricate topics into easy-to-follow tutorials.

​​​​​​​

   Connect with Author

LinkedIn

Email

​

​

bottom of page