'How can I delete the product and corresponding logs?
I want to delete all the product logs of a specific product depending on its product_id(FK). I created product logs to monitor when the product it's created and updated. I deleted a product by using its SKU. My problem is when I try to delete a product and its logs this error show Http failure response for http://localhost:8080/products/deletebysku?sku=c5c00b00-825f-45b8-80fe-1d357716a069: 405 OK. I can't delete the product if the product log has two or more same product_id(FK).
products table
products_log table
ProductService.java - Delete a product record using SKU
public void deleteProductBySku(String sku){
Products product = productsRepository.findBySkuEquals(sku);
ProductsLog productsLog = productsLogRepository.findByProductIdEquals(product.getId());
int id = product.getId();
this.delete(id);
productsLogService.delete(productsLog.getId());
}
ProductLogService - deleting a specific record by using the method deleteById() of JpaRepository
public void delete(int id)
{
productsLogRepository.deleteById(id);
}
ProductsRepository
public interface ProductsRepository extends JpaRepository<Products, Integer>
{
//find a product record using sku
Products findBySkuEquals(String sku);
}
ProductsLogRepository
public interface ProductsLogRepository extends JpaRepository<ProductsLog, Integer> {
//find a product log using productId
ProductsLog findByProductIdEquals(int id);
}
ProductsController - Delete a product record using SKU (localhost:8080/products/deletebysku?sku=samplesku)
@RequestMapping(value="/products/deletebysku", method=RequestMethod.DELETE)
@ResponseBody
private void deleteProductBySku(@RequestParam String sku)
{
productsService.deleteProductBySku(sku);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|


