How to increase the default length of a column in Hybris

Requirement

I need to increase default length of column defined in Hybris

Example

I need to increase the length of a column called “nickname” defined as part of “MyStoreCustomer” item type

Whenever we define any column as a String type in items.xml, By default Hybris defines max length of column as 255 characters. It means we can insert 255 characters max in that column.

But what if our requirement is to increase default size, let’s say increase to 500 characters ?

In this case, we need to explicitly increase the column size while defining attribute in items.xml

This article is all about how to achieve this.

There are 2 scenarios to achieve the requirement

Scenario 1
We are defining a new attribute and for this new attribute we need to provide max size as 500 characters

Scenario 2
There is already an existing attribute defined either in Hybris OOTB or as part of your project and we need to increase the max size as 

500 for this existing attribute


Let us look at the solution for both the scenarios

Scenario1

In this case, its pretty simple, as we are defining new attribute, we can mention new size of a column using “columntype” as below

  1. <itemtype code="MyStoreCustomer"
  2.                   extends="Customer"
  3.                   jaloclass="com.kb.core.jalo.MyStoreCustomer"
  4.                   autocreate="true"
  5.                   generate="true">
  6.             <attributes>
  7.                
  8.                  <attribute autocreate="true" qualifier="nickName" type="java.lang.String">
  9.                     <modifiers read="true" write="true" search="true" optional="true"/>
  10.                     <persistence type="property">
  11.                     <columntype>
  12.                             <value>varchar(500)</value>
  13.                     </columntype>
  14.                     </persistence>
  15.                 </attribute>
  16.                                
  17.             </attributes>
  18. </itemtype>
<itemtype code="MyStoreCustomer"
                  extends="Customer"
                  jaloclass="com.kb.core.jalo.MyStoreCustomer"
                  autocreate="true"
                  generate="true">
            <attributes>
                
                 <attribute autocreate="true" qualifier="nickName" type="java.lang.String">
                    <modifiers read="true" write="true" search="true" optional="true"/>
                    <persistence type="property">
                    <columntype>
							<value>varchar(500)</value>
					</columntype>
                    </persistence>
                </attribute>
                                
            </attributes>
</itemtype>


In this case, we have given the column size as 500.

Just do update system after build, You will be able to see this column with size as 500


Scenario2

This scenario is pretty complex to handle

Reason: Attribute is already defined with default size and it is also persisted in backend

Now Hybris does not allow us to alter its size through items.xml without doing initialization

Then How can we achieve this?

Well, we can still achieve this by following any one of the below 2 options

Option A

Update items.xml as stated in scenario 1 and do initialization

This is probably not a good choice as we can’t take a call for initialization if our project is already delivered to client. We can still do it by taking data backup but not a best choice.

Please avoid initialization as it destroys all the data.

Option B

Configure below code to execute during initialize and update process hook

Check Initialization-and-update-hook-up article for more details to see how to configure below method to execute during Initialization and update system.

  1. public void updateColumnsSize()
  2.     {
  3.         Connection conn = null;
  4.         PreparedStatement pstmt = null;
  5.         try
  6.         {
  7.             conn = Registry.getCurrentTenant().getDataSource().getConnection();
  8.  
  9.            
  10.             pstmt = conn.prepareStatement("alter table users alter column nickname varchar(500)");
  11.             pstmt.execute();
  12.  
  13.            
  14.  
  15.         }
  16.         catch (final SQLException e)
  17.         {
  18.             LOG.error("Unable to alter database column!");
  19.         }
  20.         finally
  21.         {
  22.             Utilities.tryToCloseJDBC(conn, pstmt, null);
  23.         }
  24. }
public void updateColumnsSize()
	{
		Connection conn = null;
		PreparedStatement pstmt = null;
		try
		{
			conn = Registry.getCurrentTenant().getDataSource().getConnection();

			
			pstmt = conn.prepareStatement("alter table users alter column nickname varchar(500)");
			pstmt.execute();

			

		}
		catch (final SQLException e)
		{
			LOG.error("Unable to alter database column!");
		}
		finally
		{
			Utilities.tryToCloseJDBC(conn, pstmt, null);
		}
}

Note:

You can load the DB value from property file to see which DB you are connecting to and then modify above code to have if else condition with appropriate query as per the DB configured.

Above query works for Sql Server DB

For MySql DB, query would be as below

  1. alter table users modify column nickname varchar(500)
alter table users modify column nickname varchar(500)

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