When we try to create a new instances of an item type programmatically, we have two options, using the java way, using new operator or the hybris way, using the model service.
ProductModel product1 = new ProductModel();
ProductModel product2 = modelService.create(ProductModel.class);
The advantages of using model service method are below:
- The model service create method will generate and assign the pk for product object.
- The create method will initialize the default values, defined in items.xml for mandatory attributes.
- While calling save all method, the object is already attached to context, and will be saved. While product1 needs to attach explicitly.
So we should always go for model service way in hybris.