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
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
30
<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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  <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

1
2
3
4
5
6
7
8
9
10
11
12
  <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

About the Author

Karibasappa G C (KB)
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