I'm trying to make a method to update the service. But 404 comes back and this is the error Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction ". What could it be?

Update ServiceService method:

  public Service update(Service service) { Service serviceToUpdate = serviceRepository.findById(service.getId()).orElseThrow(() -> new RuntimeException("Requested entity was not found")); serviceToUpdate.setId(service.getId()); serviceToUpdate.setName(service.getName()); serviceToUpdate.setAddress(service.getAddress()); serviceToUpdate.setLandmark(service.getLandmark()); serviceToUpdate.setWeb(service.getWeb()); serviceToUpdate.setPhone(service.getPhone()); serviceToUpdate.setMinAge(service.getMinAge()); serviceToUpdate.setMaxAge(service.getMaxAge()); serviceToUpdate.setPhotoRaw(service.getPhotoRaw()); serviceToUpdate.setCity(service.getCity()); serviceToUpdate.setDistrict(service.getDistrict()); serviceToUpdate.setMetroStation(service.getMetroStation()); serviceToUpdate.setSeller(service.getSeller()); return serviceRepository.save(serviceToUpdate); } 

Controller:

 @RequestMapping(value = "/service/{id}", method = RequestMethod.PUT) public ResponseEntity<String> updateService(@PathVariable("id") Long id, @RequestBody Service service) { if (serviceService.findById(id) == null) { return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); } else { service.setId(id); serviceService.update(service); return ResponseEntity.status(HttpStatus.OK).build(); } } 

In response, I get this error:

 { "status": "NOT_FOUND", "message": "Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction", "errors": [ "Requested entity was not found" ] } 
  • one
    So you yourself throw this error if the entity is not found by id. And in essence: lay out the entire glass structure, not the top - Victor
  • @Viktor So the fact of the matter is that this id is in the database. The structure is clean, this error in the postman climbs. - Pro100Denysko
  • one
    replace the request with an Optional and see what the repository returns. If there is null, then look for why null - Victor

0