What is the difference between dynamic and persisted attributes?
Dynamic Attributes allow to add custom logic to the Models attributes without the touching peristence layer.
How can I create a Dynamic Attribute?
1. Need to define the Dynamic Attribute in the items.xml File
- Add the new Dynamic Attribute
- Set the persistence type of new attribute to dynamic
- Provide a custom attributeHandler. If you do not provide the custom bean ID for attributeHandler, it is automatically generated in the following way: ClientName_longNameAttributeHandler
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<itemtype code="CarInfo"
extends="GenericItem"
jaloclass="de.hybris.tutorial.jalo.CarInfo"
autocreate="true"
generate="false">
<attributes>
<!-- Existing functionality -->
<attribute autocreate="true" qualifier="brand" type="java.lang.String">
<persistence type="property" />
</attribute>
<attribute autocreate="true" qualifier="carName" type="java.lang.String">
<persistence type="property" />
</attribute>
<attribute autocreate="true" qualifier="bodyType" type="java.lang.String">
<persistence type="property" />
</attribute>
<attribute autocreate="true" qualifier="bodyCode" type="java.lang.String">
<persistence type="property" />
</attribute>
<!-- The new Dynamic Attribute -->
<attribute type="java.lang.String" qualifier="fullName">
<persistence type="dynamic" attributeHandler="fullNameHandler" />
<modifiers read="true" write="false" optional="true" unique="false" />
</attribute>
<!-- other attributes -->
</attributes>
</itemtype>
2. Create your DynamicAttributeHandler.
The class should implement DynamicAttributeHandler interface and override the getter and setter methods. Because it doesn’t require a setter (see items.xml above) we can just throw an UnsupportedOperationException when calling the setter method.
1 | public class FullNameHandler implements DynamicAttributeHandler<String, CarInfoModel> { |
3. Register the Spring Bean
1 | <bean id="fullNameHandler" class="com.carsupplier.core.servicelayer.FullNameHandler"/> |
4. Update Running System by hac or the command line
5. Use the attribte
1 | protected void setFullName(CarInfoModel source, CarInfoData target){ |
6. Check the possible result on the frontend
Can you localize dynamic attributes?
Yes. You need to use localized type in items.xml and implement the DynamicLocalizedAttributeHandler
Some example from items.xml:
1 | <!-- other attributes --> |
Also don’t forget to register the Spring Bean and do the other stuff.
Ok. Let’s look at the code. What’s wrong with this example of using a dynamic attribute?
Just find the main issue here.
1 |
|
The main issue here, as you can see is that this code has a significant performance impact during the call the method. Dynamic attributes are expected to be fast and simple. They shouldn’t contain any complicated logic or perform expensive calls.
Created based on official documentation