Adding a new attribute in Hybris Model

All object models extends ItemModel class, there are such steps to add a new attribute where database will happen to have it created in the target table. So simply at work to test it out I created a field called “Profession” in the registration page in “yacceleratorstorefront” module. I am assuming you’ve imported all corresponding projects into your workspace.

Steps

1. Open CustomerModel class using CTRL+SHIFT+H

2.First we need to create a constant in a String instance. The variable name as you know has to be in capital letters  example;

public static final String PROFESSION = "profession";

3. Then we need to create a variable for the value the name needs to start with underscore example;

private String _profession;

4. When above steps are completed, we will create getter and setter methods which will differ than the relevant methods we know. While implementing those methods we will use above constant and variable, so lets get started with the getter method, example;

public String getProfession(){
		if (this._profession != null)
		{
			return _profession;
		}
		return _profession = getPersistenceContext().getValue(PROFESSION, _profession);
	}

As above you see first of all we check whether the variable “_profession” is empty, if it is we return value by giving persistence context PROFESSION constant and _profession variables

5.Setter method is still different than we know the one traditional so the example;

public void setProfession(final String value)
	{
		_profession = getPersistenceContext().setValue(PROFESSION, value);
	}

as you see above we give persistence context PROFESSION constant and given value to set the value to the regarding variable

6.Accordingly open the class called “RegisterData”

7. Create a string instance named profession

8. Create setter and getter methods for profession instance

9. Later on open the class called “RegisterForm”

10. Create a string instance named profession

11. Create setter and getter methods for profession instance

12.Now we need to add some validation, to do so open the class called “RegistrationValidator”

13.we first of all need to add a variable in validate method, it will fail saying it is not in use dismiss the error for now till we write validation code for it example;

final String profession = registerForm.getProfession();

14. then go below and add this line example – you may improve it upon your own will;

//validation for profession
		if (StringUtils.isEmpty(profession))
		{
			errors.rejectValue("profession", "profession.pwd.invalid");
		}

15.open the class called “AbstractRegisterPageController” and in the processRegisterUserRequest method add the following line

data.setProfession(form.getProfession());

16.Last of all, open the class file called “DefaultCustomerFacade” and go to the register method

17. add the below line before the “getCustomerAccountService” method, otherwise the value will not be saved

newCustomer.setProfession(registerData.getProfession());

18. Open commercefacades-beans in “commercefacades\resources”

19.go to the starts with “<bean class=”de.hybris.platform.commercefacades.user.data.RegisterData”>” and add below line

<property name="profession" type="String" />

19. issue the command ant clean all

 

Leave a Reply

Your email address will not be published. Required fields are marked *