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" ] }