cancel
Showing results for 
Search instead for 
Did you mean: 

Update product with modelService.save() fails

Former Member
0 Kudos

Hi there,

I implemented my own facade layer to manage the products in hybris. At the time I was able to create and to receive products, but unfortunately I can't update products...

If I try to update a product with modelService.save() method, I receive the following error message:

 SEVERE: Servlet.service() for servlet [DispatcherServlet] in context with path [/storefront] threw exception [Request processing failed; nested exception is de.hybris.platform.servicelayer.exceptions.ModelSavingException: [de.hybris.platform.servicelayer.interceptor.impl.UniqueAttributesInterceptor@6cd408b1]:ambiguous unique keys {catalogVersion=CatalogVersionModel (8796093284953), code=99432499} for model ProductModel (<unsaved>) - found 1 item(s) using the same keys] with root cause
 de.hybris.platform.servicelayer.interceptor.InterceptorException: [de.hybris.platform.servicelayer.interceptor.impl.UniqueAttributesInterceptor@6cd408b1]:ambiguous unique keys {catalogVersion=CatalogVersionModel (8796093284953), code=99432499} for model ProductModel (<unsaved>) - found 1 item(s) using the same keys
     at de.hybris.platform.servicelayer.interceptor.impl.UniqueAttributesInterceptor.onValidate(UniqueAttributesInterceptor.java:159)
     at de.hybris.platform.servicelayer.internal.model.impl.wrapper.ModelWrapper.invokeValidateInterceptors(ModelWrapper.java:272)
     at de.hybris.platform.servicelayer.internal.model.impl.wrapper.ModelWrapper.validate(ModelWrapper.java:216)

It looks like modelService was trying to create a new product instead of updating a product...

The implementation looks like that:

 final ProductModel productModel = productService.getProductForCode(product.getCode());
 
 productModel.setManufacturerAID(product.getManufacturerAID());
 productModel.setDescription(product.getDescription());
 productModel.setApprovalStatus(checkApprovalStatus(product.getApprovalStatus()));
 productModel.setCatalogVersion(catalogVersionService.getCatalogVersion(product.getCatalogName(), product.getCatalogVersion()));
 
 modelService.save(productModel)

Does anybody an idea how to solve this problem?

greetz

Accepted Solutions (1)

Accepted Solutions (1)

andyfletcher
Active Contributor
0 Kudos

Probably what is happening is that you are retrieving the product from a different catalog version and then trying to change its catalog version to something where that product already exists.

e.g. product '99432499' exists in both your staged and online catalog versions. You retrieve it from the staged version (because you haven't specified which catalog version to look in) and then try to save it into the online version. There is already a product with that code there but with a different pk.

I would try this instead.

 CatalogVersionModel catalogVersion = catalogVersionService.getCatalogVersion(product.getCatalogName(), product.getCatalogVersion());
 ProductModel productModel = productService.getProductForCode(catalogVersion, product.getCode());

 productModel.setManufacturerAID(product.getManufacturerAID());
 productModel.setDescription(product.getDescription());
 productModel.setApprovalStatus(checkApprovalStatus(product.getApprovalStatus()));

 modelService.save(productModel);

Answers (3)

Answers (3)

0 Kudos

If product is already exiting one then it will try to update the same product , if does not exist then it will create a new one. Agreed with answer of ..

Former Member
0 Kudos

Hello,

the product with the code "99432499" already exists in the catalog version with pk 8796093284953.

Please be aware in the same catalog version, the product code must be unique.

Regards,

Lin

Former Member
0 Kudos

It looks like the product with code '99432499' in catalog '8796093284953' already exists.

You may want to do a check to ensure it already exists first, then update that one.

Try using the hMC to find the product to make sure verify the product does exist.