[英]How to handle exceptions using EntityManager in Spring Boot
I have dao layer: 我有道层:
@Transactional
public class DatabaseCollectionDao implements IDatabaseCollectionDao {
@PersistenceContext
private EntityManager entityManager;
@Override
public void create(Collection collection) {
entityManager.persist(collection);
}
}
It works correctly but: 它可以正常工作,但是:
I am trying to try/catch it inside create method: 我试图在create方法中尝试/捕获它:
@Override
public void create(Collection collection) {
try{
entityManager.persist(collection);
} catch (SQLIntegrityConstraintViolationException e){
//do smth
}
}
But Intellij says that It is never thrown . 但是Intellij说它从不抛出 。
When I try to try/catch Exception i have UnexpectedRollbackException . 当我尝试尝试/捕获异常时,我遇到了UnexpectedRollbackException 。
How to handle exceptions using JPA entityManager? 如何使用JPAEntityManager处理异常?
update : An attempt to remove @Transactional gave nothing 更新 :尝试删除@Transactional没有任何作用
PS To be sure i tried to try/catch it in higher layers. PS可以肯定的是,我试图在更高的层次上尝试/捕捉它。 I don't know what i can try more to solve it.
我不知道我可以尝试解决什么。
create customException handler extend ResponseEntityExceptionHandler . 创建customException处理程序扩展ResponseEntityExceptionHandler。 @ExceptionHandler(ConstraintViolationException::class) fun handleConstraintViolation(ex: ConstraintViolationException, request: WebRequest): ResponseEntity {} this kotlin snippet u can convert to java easily –
@ExceptionHandler(ConstraintViolationException :: class)有趣的handleConstraintViolation(例如:ConstraintViolationException,请求:WebRequest):ResponseEntity {}您可以轻松将Kotlin片段转换为Java –
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.