I have some tables in my database and I want to get information about wrong requests to the database. In case If I'm trying to save entity with wrong foreigns keys, I want to get detail information about these keys.
For example:
2020-03-25 18:37:37.595 ERROR 9788 --- [nio-8090-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper : ERROR: insert or update on table "student" violates foreign key constraint "student_fkey_to_specialty"
Detail: Key (specialtykey)=(2) is not present in table "specialty".
I tried to solve with this code, but I get other information.
could not execute statement; SQL [n/a]; constraint [student_fkey_to_specialty]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
my code:
@PostMapping
public void saveStudent(@RequestBody StudentDTO studentDTO) {
if(studentDTO!=null){
try {
studentService.save(studentDTO);
}catch (Exception|Error e){
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getLocalizedMessage(),e );
}
}
}