title: Custom ConstraintViolationException in spring-boot
body: <p>How I catch exception for this ConstraintViolationException :</p>
  
  <pre><code>{
      "timestamp": "2019-05-31T07:23:03.419+0000",
      "status": 500,
      "error": "Internal Server Error",
      "message": "could not execute statement; SQL [n/a]; constraint [fkitcllv8yn2id9lj3rmw3wskhd]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement",
      "path": "/pelajar"
  }
  </code></pre>
  
  <p>This Excaption is happen, when I try to deleting a record. But that record still used as reference by other record in other table.</p>
  
  <p>I Have 2 CustomExceptionHandle :
   the 1st for catch general exception and 
   the 2nd for catch ConstraintViolation exception. </p>
  
  <p>But in my program, each time ConstraintViolation trigger-up, the handleAllExceptions will be executed.
  My expectation is  handleConstraint function should be executed.</p>
  
  <pre><code>@ControllerAdvice
  @RestController
  public class CustomResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
  
      @Order(Ordered.HIGHEST_PRECEDENCE)
      @ExceptionHandler(value = {ConstraintViolationException.class})
      public  ResponseEntity&lt;Object&gt; handleConstraint(ConstraintViolationException ex, 
              WebRequest request ) {
  
          ExceptionResponse exceptionResponse = new ExceptionResponse(new Date(), "Record still have reference from other table",
                  request.getDescription(false));
          return new ResponseEntity(exceptionResponse, HttpStatus.BAD_REQUEST);       
      }
  
  
      @Order(Ordered.LOWEST_PRECEDENCE)
      @ExceptionHandler(value = {Exception.class})
      public final ResponseEntity&lt;Object&gt; handleAllExceptions(Exception ex, WebRequest request) {
          ExceptionResponse exceptionResponse = new ExceptionResponse(new Date(), ex.getMessage()+" Custom Error",
                  request.getDescription(false));
          return new ResponseEntity(exceptionResponse, HttpStatus.INTERNAL_SERVER_ERROR);
      }
  }
  </code></pre>
  
  <p>But, If I remove handleAllExceptions the ConstraintViolationException will be catch by handleConstraint function.</p>
  
  <p>Can anyone give me advice ?</p>
  
  <p>Thank You.<br>
  Alt...</p>