- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我如何捕获此 ConstraintViolationException 的异常:
{
"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"
}
当我尝试删除记录时,会发生此说明。但该记录仍被其他表中的其他记录引用。
我有 2 个 CustomExceptionHandle : 第一个用于捕获一般异常和 第二个用于捕获 ConstraintViolation 异常。
但在我的程序中,每次ConstraintViolation触发时,handleAllExceptions都会被执行。我的期望是应该执行handleConstraint函数。
@ControllerAdvice
@RestController
public class CustomResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
@Order(Ordered.HIGHEST_PRECEDENCE)
@ExceptionHandler(value = {ConstraintViolationException.class})
public ResponseEntity<Object> 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<Object> 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);
}
}
但是,如果我删除handleAllExceptions,ConstraintViolationException 将被handleConstraint 函数捕获。
谁能给我建议吗?
谢谢。
替代...
最佳答案
似乎您已在 CustomResponseEntityExceptionHandler
中导入了 javax.validation.ConstraintViolationException
而不是 org.hibernate.exception.ConstraintViolationException
。如果未正确导入,请导入org.hibernate.exception.ConstraintViolationException
。
关于java - spring-boot 中的自定义 ConstraintViolationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56392155/
我的服务中有一个方法: @Transactional public MyResponse create(UserCredentials cred) { User user =
mysql> alter table metakey add constraint Name unique(name); mysql> desc metakey; +-------+--------
我正在尝试使用 JPA 实体和 @SequenceGenerator 将一些数据添加到数据库中。但似乎这个生成器没有达到预期的效果,我不知道为什么。 Hibernate: call next valu
当我尝试使用 hibernate 进行 saveOrUpdate 时,出现 ConstraintViolationException。当我为用户插入一个全新的对象时,保存工作正常,但当我尝试更新时却失
我有 2 个对象实体(用户和电话),它们应该具有多对多关系。 User.java //all columns @ManyToMany(cascade = CascadeType.MERGE, fetc
这似乎应该是一个非常简单的问题,或者至少有一个简单的答案。但是 - 我真的不是数据库专家,而且我在 Hibernate 学习曲线上还差得很远。也就是说,这是设置: 考虑两个实体之间的单向多对多关系,从
我在 JEE 应用程序中有一些 SessionBean,并且喜欢使用 BeanValidation 来验证我的参数。因此整个验证过程是通用的并封装在一个地方。 我看到的唯一缺点是客户端得到一个 EJB
尽管我在日志中看到它,但我似乎无法捕捉到它。 实体 @Column(unique = true) private String email; 我正试图像这样捕获它 try { memb
我有一个具有多字段唯一约束的持久类。但是定义的唯一约束对我来说还不够,因为在一个字段上,那些不相等但相似的值也是唯一的。 我实现了一个 checkUniqueConstraint 方法。在 DAO 类
我正在使用 JSF+JPA 我没有修复这个错误: javax.validation.ConstraintViolationException: Bean Validation constraint(s
我正在编写一个 Spring 应用程序,似乎当我遇到数据库错误时,有时 Hibernate 的 ConstraintViolationException有时会抛出 Spring 的 DataInteg
如何获取导致org.hibernate.exception.ConstraintViolationException的字段名称?检查唯一约束的唯一可靠方法是事务提交,因此即使我在抛出异常之前检查它。因
我正在开发一个基于 Spring Boot 的 REST API。我正在使用自定义 ConstraintValidator 注释验证输入实体。我的问题是我无法在响应中返回 ConstraintViol
在 neo4j 2.0 中,如果您尝试对已经具有约束的索引施加约束,则会抛出 ConstraintViolationException,这是由 AlreadyConstrainedException
我正在尝试改进我在 spring MVC 应用程序中处理来自 DAO/服务层的异常的方式。我在下面使用的方法非常有效,不会导致 500 错误: Controller 代码 @RequestMappin
我有一个名为 add() 的服务方法,它用 @Transactional 注释。 我调用它,但是当在相应的 DAO 方法中发生 ConstraintViolationException 时,它会回滚事
我的数据库中有两个表:一个用于持久化用户,另一个用于保存他们的权限。 问题是,当我尝试删除用户时,必须删除权限,这样我才能避免收到违反约束的异常。我为此使用了 JPA 级联类型.ALL 和 orpha
我正在尝试将记录保存在具有多个唯一约束的表中。其中之一称为 UK_ESTABLISHMENT_REGISTERS_003,基于 2 列。 当用户尝试在表中输入重复值时,会引发约束冲突异常,但我无法检索
我正在使用 Play Framework,并具有以下代码: import org.hibernate.exception.ConstraintViolationException; ... publi
我正在努力解决这里的一个小问题。基本上我们有一个表,其中列不是 PK 但需要是唯一的,因此我们在其上添加了唯一性约束。现在,在我们的服务层中,我们希望捕获当有人创建打破该唯一性约束的记录时生成的异常,
我是一名优秀的程序员,十分优秀!