【发布时间】:2017-06-27 18:25:18
【问题描述】:
我正在尝试处理异常
这是堆栈跟踪;
org.hibernate.exception.ConstraintViolationException: could not execute statement
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:59)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
...
...
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'testemail@gmail.com' for key 'UK_n7ihswpy07ci568w34q0oi8he'
当我尝试使用 getMessage() 方法获取消息时,得到的消息是来自 ConstraintViolationException 的“无法执行语句”,
但我想要的是得到 来自 MySQLIntegrityConstraintViolationException 的“密钥 'UK_n7ihswpy07ci568w34q0oi8he' 的重复条目 'testemail@gmail.com'”消息。
这是我的捕捉过程
catch(MySQLIntegrityConstraintViolationException e){
e.printStackTrace();
message = "MySQLIntegrity,\n Duplicate Entry, \n" + e.getMessage();
}
catch(ConstraintViolationException e){
e.printStackTrace();
message = "ConstraintViolation,\n Duplicate Entry, \n" + e.getMessage();
}
catch (Exception e) {
e.printStackTrace();
message = "Exception rule,\n" + e.getMessage();
}
【问题讨论】:
标签: hibernate spring-mvc