Model service create method vs new operator

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:

  1. The model service create method will generate and assign the pk for product object.
  2. The create method will initialize the default values, defined in items.xml for mandatory attributes.
  3. 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.

What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s