这是我的代码片段:
@Stateless
public class mySLSB {
@PersistenceContext(unitName = "db")
private EntityManager myEntityManager;
public void crud(MyEntity myEntity) throws MyException {
myEntityManager.merge(myEntity);
}
}
但是,此合并会导致 ConstraintViolationException,它不会抛出 MyException(在调用 servlet 中捕获)。
捕获 hibernate 异常的最佳方法是什么?
最佳答案
However this merge can cause a ConstraintViolationException, which does not throw MyException (which is caught in the calling servlet). What is the best way to catch hibernate exceptions?
当使用 JPA 和 EntityManager API 时,您将得到一个 javax.persistence.RollbackException
并以 ConstraintViolationException
为原因。所以捕获 RollbackException
并使用 getCause()
来检查它。
关于hibernate - 在 SLSB 中使用 EJB3 和实体管理器处理 ConstraintViolationException 的最佳方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4053931/