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
- <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>
<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.
- 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);
- }
- }
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
- alter table users modify column nickname varchar(500)
alter table users modify column nickname varchar(500)
Hi Karibasppa,
can you suggest me As hybris tester what and all concepts to learn . how to good be in topics , if you have any study material please send on mail , it should be great for me .
HYBRIS
Hi Karibasappa, Good Explanation.
Can we alter the column size using IMPEX Script? How?
Hi,
We can directly run the above query in HAC?
Yes, we can using “SQL Query” option under flexible search console
Just changing the items.xml and doing system update (can be done on Production environment) is not enough ? I thought System update will take care of changes in the definition of Item type in items.xml fie ?
No, that’s where scenario 2 , option “B” in the above article rescues
Very good explanation…..thanks a lot plz keep updating new things
Sir, I want to add the new field in registration page on the given hybris website.what are changes are to be made.plse let me known.