Commerce, Java

Deleting an attribute from Hybris Database

In a typical Hybris project during the development, we come across deleting/modifying attribute. When we modify the attribute name in items.xml and does a system update, Hybris will create a new column in database instead of updating the existing column. Below process will help us in modifying the attribute without any side effects. We are taking an example of modifying attributeOld of myTable to attributeNew.

Step 1: Modify your items.xml file.

<itemtype code="MyTable …….>
  <attributes>
    <attribute qualifier=" attributeNew "………>
      .........
    </attribute>
  </attributes>
</itemtype>

Note: Performing further steps will delete the data from your database. If you don’t want to delete the data, see Step 7 once.

Step 2: Write a remove old attribute descriptor impex.

REMOVE AttributeDescriptor; qualifier[unique = true]; enclosingType(code)[unique = true]
; attributeOld ; MyTable

Step 3: Remove the column from SQL Database using a groovy script.

import org.springframework.jdbc.core.JdbcTemplate
JdbcTemplate jdbcTemplate = spring.getBean("jdbcTemplate")
try {
&nbsp;&nbsp;&nbsp; int rows = jdbcTemplate.update("ALTER TABLE mytable DROP COLUMN attributeold;")
} catch (Exception e) {
&nbsp;&nbsp;&nbsp; println(String.format("'%s'", e.getMessage()))
}

Step 4: Run ant clean all and start the server.

Step 5: Run the removal impex (Step 2) and groovy script (Step 3).

Step 6: Perform a system update.

Step 7 (Optional): If you want to copy the same existing data, Before performing Step 5, Run the below export script and import by modifying the attribute name.

INSERT_UPDATE Mytable;code[unique=true];attributeOld

 

About The Author

Leave a Reply

*