Wednesday, August 26, 2015

Adding a column to a table in Hybris database

What will you do if you want to add a new column to a table in Hybris database?

Let us take an example of customerReview Table.

If you run the following query in Hybris console , you will get the default columns already available -


 
Now if you wish to add a new column say STANDING to this CUSTOMERREVIEW table, do the following -

1) Add the following code to commercefacades-beans.xml available in commercefacades extension, note here standing property of type string has been added to the already existing code.

<bean class="de.hybris.platform.commercefacades.product.data.ReviewData">
<property name="id" type="String"/>
<property name="headline" type="String"/>
<property name="comment" type="String"/>
<property name="rating" type="Double"/>
<property name="date" type="java.util.Date"/>
<property name="alias" type="String"/>
<property name="standing" type="String"/>
<property name="principal" type="de.hybris.platform.commercefacades.user.data.PrincipalData"/>
</bean>
 
 
2) Add the following code to customerreview-items.xml, available in customerreview extension. 

<attribute type="java.lang.String" qualifier="standing">
<description>
Standing will be decided based upon the no of reviews.
</description>
<modifiers initial="true" optional="false" />
<persistence type="property" />
</attribute>


3) Run Ant Clean All and start the hybris server.


4) After this all the Data and Model files related to the newly added property (standing) will be created automatically, you can verify this by opening the following files -> CustomerReviewModel and ReviewData.


5) Go to Admin Console -> Platform -> Update and click on Update Button.




6) Now if you again run the select query, you will find the newly added column :)




In this way you can modify the tables and include your own columns if you want to implement some customized logic.

You can also experiment and add a completely new table to the database.

No comments:

Post a Comment

Home