当我调用JPA的save()时,它给出了ConstraintViolation异常的异常,但它在嵌套异常内部。
第一个是 IllegalStateException ,然后是 NestedServletException , DataIntegrotyViolationException , ConstraintViolationException , SQLServerException > 有
com.microsoft.sqlserver.jdbc.SQLServerException: Violation of UNIQUE KEY constraint 'uk_urmapping_uname_role'. Cannot insert duplicate key in object 'dbo.employee_role_mapping'. The duplicate key value is (wxy ., ABC).
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:259) ~[mssql-jdbc-6.4.0.jre8.jar:na]
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1547) ~[mssql-jdbc-6.4.0.jre8.jar:na]
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:548) ~[mssql-jdbc-6.4.0.jre8.jar:na]
在我添加的CustomExceptionHandler类中
@ExceptionHandler({ GenericJDBCException.class, SQLException.class, DuplicateKeyException.class,
ConstraintViolationException.class, JDBCException.class })
public ResponseEntity<Object> processSqlExceptions(SQLException e, WebRequest request) {
logger.error("SQL Error : ", e);
logger.warn("SQL Error : ", e);
ErrorResponse error = new ErrorResponse("600", e.getMessage(), tracer.currentSpan().context().spanIdString());
return handleExceptionInternal(e, error, new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
}
我试图处理一些异常,但仍然无法处理异常。
答案 0 :(得分:0)
它是DataIntegrityViolationException
,因此您应该在处理程序中添加它。
@ExceptionHandler(value = {DataIntegrityViolationException.class})
答案 1 :(得分:0)
如果您使用hibernate
,它将抛出它自己的ConstraintViolationException
版本。
因此,请确保您处理org.hibernate.exception.ConstraintViolationException
而不是javax.validation.ConstraintViolationException
(或两者)。