cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Save a model without saving its referred models

0 Likes
387

Hi Experts,

Is there a good way to save a model without causing the implicit saving of referred models?

For instance, a PriceRowModel contains a product attribute that is a reference to a ProductModel.
Due to that, in the following code example (very simplified) the product is implicitly being saved when I save the product row:

 public void updateProduct(String code, ProductChangesBean changes) {
     ProductModel product = productService.getProductForCode(catVersion, code);

     // ...do some changes to the product...
     updateProductUnit(product, changes.getUnitC());
     // ...do more changes to the product...

     modelService.save(product);
 }

 public void updateProductUnit(ProductModel product, UnitModel unit)
     if (unit == null) {
         return;
     }

     product.setUnit(unit);
     for (PriceRowModel priceRow : product.getEurope1Prices()) {
         priceRow.setUnit(unit);
         modelService.save(priceRow);   // <== this is saving priceRow's and product
     }
 }

I would like updateProductUnit(..) to save only the price rows without saving the product referred by them.

I have tried detaching the product before saving the price row, but the product is still being saved with it.
I also tried with not saving the price row, to see if the saving of the product will "cascade" that. But it doesn't.
Any other ideas?

Cheers,
Carlos

Accepted Solutions (0)

Answers (0)