如何从Hibernate Exception中提取constraintName

时间:2014-07-28 08:02:16

标签: java hibernate exception

如何使用java,

从异常中提取constraintName
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);
            }
        }

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

我的问题是如何从抛出的异常中获取约束名称。

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

感谢

1 个答案:

答案 0 :(得分:5)

试试这个:

try {

} catch(HibernateException e) {
  if(e instanceof ConstraintViolationException) {
    ConstraintViolationException ce = (ConstraintViolationException) e;
    String constraintName = ce.getConstraintName();
  }
}