Redeclare in items.xml
Introduction
In Java , we have a concept called variable hiding which means variable with the same name is defined in both parent and child classes.
In such cases,variable from Parent will be inherited but it will be hidden in the Child class as the Child class also has the same variable.
we can also change the variable data type in Child class keeping the same variable name.
For more details on Variable hiding in Java, Click here
Similar to Variable Hiding in Java, we have a concept called “Re declaring” the attributes” in Hybris.
Re declaring the attributes in Hybris
Sometimes it is required to re declare the attribute in the child item type for various reasons.
It could be to change the Data type of an attribute or make an attribute as read only etc.
So We can re declare the same attributes in the Child item type to change such behavior
Examples:
We can change the modifier from read=true to read=false
We can make attribute as unique.
We can add write=false.
We can also change the type of the attribute but only to subtypes.
Example 1:
Adding unique flag for the redefined attribute.
We can see that isocode attribute is re defined in Country item type even though its defined in its Parent item type called C2Litem.
- <itemtype code="C2LItem" extends="GenericItem" jaloclass="de.hybris.platform.jalo.c2l.C2LItem"
- autocreate="true" abstract="true" generate="true">
- <attributes>
- <attribute autocreate="true" qualifier="active" type="java.lang.Boolean">
- <persistence type="property"/>
- <modifiers read="true" write="true" search="true" optional="false"/>
- <defaultvalue>Boolean.valueOf(false)</defaultvalue>
- </attribute>
- <attribute autocreate="true" qualifier="isocode" type="java.lang.String">
- <persistence type="property"/>
- <modifiers read="true" write="true" search="true" optional ="false"/>
- <custom-properties>
- <property name="hmcIndexField">
- <value>"thefield"</value>
- </property>
- </custom-properties>
- </attribute>
- </attributes>
- </itemtype>
- <itemtype code="Country" extends="C2LItem" jaloclass="de.hybris.platform.jalo.c2l.Country"
- autocreate="true" generate="true">
- <deployment table="Countries" typecode="34"/>
- <attributes>
- <attribute qualifier="isocode" type="java.lang.String"
- redeclare="true" generate="false">
- <modifiers read="true" write="true" search="true" optional="false" unique="true"/>
- </attribute>
- </attributes>
- </itemtype>
<itemtype code="C2LItem" extends="GenericItem" jaloclass="de.hybris.platform.jalo.c2l.C2LItem" autocreate="true" abstract="true" generate="true"> <attributes> <attribute autocreate="true" qualifier="active" type="java.lang.Boolean"> <persistence type="property"/> <modifiers read="true" write="true" search="true" optional="false"/> <defaultvalue>Boolean.valueOf(false)</defaultvalue> </attribute> <attribute autocreate="true" qualifier="isocode" type="java.lang.String"> <persistence type="property"/> <modifiers read="true" write="true" search="true" optional ="false"/> <custom-properties> <property name="hmcIndexField"> <value>"thefield"</value> </property> </custom-properties> </attribute> </attributes> </itemtype> <itemtype code="Country" extends="C2LItem" jaloclass="de.hybris.platform.jalo.c2l.Country" autocreate="true" generate="true"> <deployment table="Countries" typecode="34"/> <attributes> <attribute qualifier="isocode" type="java.lang.String" redeclare="true" generate="false"> <modifiers read="true" write="true" search="true" optional="false" unique="true"/> </attribute> </attributes> </itemtype>
We have re defined the attribute “isocode” in the Country item type and we have added unique=”true” to make sure that it will be unique.
Since we are redefining the attribute, we have to make redeclare=”true”.
Example2 :
Changing the type of redefined attribute.
We have defined the attribute called “entries” in the AbstractOrder item type
It has been re defined in Cart as well as Order item type.
Entries is available in AbstractOrder as a List of AbstractOrderEntry
It is re defined in Cart as a List of CartEntry
- <collectiontype code="CartEntryCollection" elementtype="CartEntry" autocreate="true" generate="false"
- type="list"/>
- <collectiontype code="OrderEntryCollection" elementtype="OrderEntry" autocreate="true" generate="false"
- type="list"/>
- <itemtype code="Cart" extends="AbstractOrder" jaloclass="de.hybris.platform.jalo.order.Cart"
- autocreate="true" generate="true">
- <deployment table="Carts" typecode="43"/>
- <attributes>
- <attribute autocreate="true" redeclare="true" qualifier="entries"
- type="CartEntryCollection"/>
- <attribute type="java.lang.String" qualifier="sessionId">
- <persistence type="property"/>
- <modifiers read="true" write="true"/>
- </attribute>
- </attributes>
- </itemtype>
<collectiontype code="CartEntryCollection" elementtype="CartEntry" autocreate="true" generate="false" type="list"/> <collectiontype code="OrderEntryCollection" elementtype="OrderEntry" autocreate="true" generate="false" type="list"/> <itemtype code="Cart" extends="AbstractOrder" jaloclass="de.hybris.platform.jalo.order.Cart" autocreate="true" generate="true"> <deployment table="Carts" typecode="43"/> <attributes> <attribute autocreate="true" redeclare="true" qualifier="entries" type="CartEntryCollection"/> <attribute type="java.lang.String" qualifier="sessionId"> <persistence type="property"/> <modifiers read="true" write="true"/> </attribute> </attributes> </itemtype>
It is re defined in Order as a List of OrderEntry
- <collectiontype code="CartEntryCollection" elementtype="CartEntry" autocreate="true" generate="false"
- type="list"/>
- <collectiontype code="OrderEntryCollection" elementtype="OrderEntry" autocreate="true" generate="false"
- type="list"/>
- <itemtype code="Order" extends="AbstractOrder" jaloclass="de.hybris.platform.jalo.order.Order"
- autocreate="true" generate="true">
- <deployment table="Orders" typecode="45" propertytable="OrderProps"/>
- <attributes>
- <attribute autocreate="true" redeclare="true" qualifier="entries"
- type="OrderEntryCollection"/>
- </attribute
- </itemtype>
<collectiontype code="CartEntryCollection" elementtype="CartEntry" autocreate="true" generate="false" type="list"/> <collectiontype code="OrderEntryCollection" elementtype="OrderEntry" autocreate="true" generate="false" type="list"/> <itemtype code="Order" extends="AbstractOrder" jaloclass="de.hybris.platform.jalo.order.Order" autocreate="true" generate="true"> <deployment table="Orders" typecode="45" propertytable="OrderProps"/> <attributes> <attribute autocreate="true" redeclare="true" qualifier="entries" type="OrderEntryCollection"/> </attribute </itemtype>
Note:
While re defining, If its required to change the data type of an attribute, it can be changed to its Sub Type only otherwise we will get
the build error as “The return type is incompatible”.
In the above case, OrderEntry and CartEntry both are the sub types of AbstractOrderEntry.
Hence it’s allowed to change from List of AbstractOrderEntry to List of CartEntry and List of OrderEntry
Hi, I am a beginner and i found your site content is great and easily understandable. My doubt is, can we also create the new attribute for an existing OOTB item type and populate it. Can you also explain how we can implement , adding a custom message for an order . Thank you!
Can uniqueness can be re-declared in subtype?
I believe SAP commerce will be unique defined for attribute redeclared in subtypes.
can u confirm?
“thefield”
Could you please explain the meaning of above code that you defined in *-item.xml?
if we don’t write jalo class in itemstypes tag (items.xml) file will it creates java class ?
Yes, still it creates with default name.
looks like while redeclare , deployment table is mandatory? it contradicts the deployment type should be avoided for subtype or for ” new item type by extending it with existing item type”. Could you please clarify
No, for redeclare Deployment is not mandatory, If you think so, please explain why do you think it has to be mandatory ?