如何从Hibernate Exception中提取constraintName

Jav*_*ons 1 java hibernate exception

如何constraintName使用java从异常中提取,

catch(HibernateException e)
        {
            if (ExceptionChecker.isUniqueConstraintViolated(e)) 
            {
                if (log.isDebugEnabled()) {

                    log.debug("save HibernateException" + e.getMessage());

                }

            }
            else
            {   
                throw ExceptionHandler.handleHibernateException(this.getClass().toString(), log, e);
            }
        }
Run Code Online (Sandbox Code Playgroud)

上面是我的java代码捕获异常并且它执行了所需的异常,但是我得到了带有约束名称的约束违反异常,

我的问题是如何从抛出的异常中获取约束Name.

我试过这种方式,但它没有用 e.getMessage().split("**")[1]).toString()

谢谢

Mag*_*and 5

试试这个:

try {

} catch(HibernateException e) {
  if(e instanceof ConstraintViolationException) {
    ConstraintViolationException ce = (ConstraintViolationException) e;
    String constraintName = ce.getConstraintName();
  }
}
Run Code Online (Sandbox Code Playgroud)