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.

  1. <itemtype code="C2LItem" extends="GenericItem" jaloclass="de.hybris.platform.jalo.c2l.C2LItem"
  2.                   autocreate="true" abstract="true" generate="true">
  3.       <attributes>
  4.            <attribute autocreate="true" qualifier="active" type="java.lang.Boolean">
  5.                 <persistence type="property"/>
  6.                 <modifiers read="true" write="true" search="true" optional="false"/>
  7.                 <defaultvalue>Boolean.valueOf(false)</defaultvalue>
  8.            </attribute>
  9.            <attribute autocreate="true" qualifier="isocode" type="java.lang.String">
  10.                 <persistence type="property"/>
  11.                 <modifiers read="true" write="true" search="true" optional ="false"/>
  12.                 <custom-properties>
  13.                 <property name="hmcIndexField">
  14.                      <value>"thefield"</value>
  15.                  </property>
  16.                  </custom-properties>
  17.             </attribute>
  18.       </attributes>
  19. </itemtype>
  20.  
  21. <itemtype code="Country" extends="C2LItem" jaloclass="de.hybris.platform.jalo.c2l.Country"
  22.                   autocreate="true" generate="true">
  23.             <deployment table="Countries" typecode="34"/>
  24.        <attributes>
  25.                <attribute qualifier="isocode" type="java.lang.String"
  26.                            redeclare="true" generate="false">
  27.                <modifiers read="true" write="true" search="true" optional="false" unique="true"/>
  28.                 </attribute>
  29.         </attributes>
  30. </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

  1.   <collectiontype code="CartEntryCollection" elementtype="CartEntry" autocreate="true" generate="false"
  2.                         type="list"/>
  3.         <collectiontype code="OrderEntryCollection" elementtype="OrderEntry" autocreate="true" generate="false"
  4.                         type="list"/>
  5. <itemtype code="Cart" extends="AbstractOrder" jaloclass="de.hybris.platform.jalo.order.Cart"
  6.             autocreate="true" generate="true">
  7.             <deployment table="Carts" typecode="43"/>
  8.    <attributes>
  9.       <attribute autocreate="true" redeclare="true" qualifier="entries"    
  10.                           type="CartEntryCollection"/>
  11.                 <attribute type="java.lang.String" qualifier="sessionId">
  12.                       <persistence type="property"/>
  13.                       <modifiers read="true" write="true"/>
  14.                 </attribute>
  15.    </attributes>
  16. </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

  1.   <collectiontype code="CartEntryCollection" elementtype="CartEntry" autocreate="true" generate="false"
  2.                         type="list"/>
  3.         <collectiontype code="OrderEntryCollection" elementtype="OrderEntry" autocreate="true" generate="false"
  4.                         type="list"/>
  5. <itemtype code="Order" extends="AbstractOrder" jaloclass="de.hybris.platform.jalo.order.Order"
  6.                   autocreate="true" generate="true">
  7.         <deployment table="Orders" typecode="45" propertytable="OrderProps"/>
  8.    <attributes>
  9.         <attribute autocreate="true" redeclare="true" qualifier="entries"  
  10.                                                  type="OrderEntryCollection"/>
  11.    </attribute
  12. </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

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on