Art*_*oly 7 java spring hibernate
在我的应用程序中,当提出违规密钥时,我想获得约束名称,但我找不到任何获取此信息的方法."getMessage()"返回的消息非常概括,我需要有关错误的更多信息,以便向最终用户发出可自定义的错误消息.
堆栈跟踪:
84732 [http-8080-1] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 0, SQLState: 23505
84732 [http-8080-1] ERROR org.hibernate.util.JDBCExceptionReporter - ERROR: duplicate key value violates unique constraint "ix_tb_oferta_vaga"
Detalhe: Key (cd_pj, cd_curso)=(680, 29) already exists.
187405 [http-8080-1] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 0, SQLState: 23505
187405 [http-8080-1] ERROR org.hibernate.util.JDBCExceptionReporter - ERROR: duplicate key value violates unique constraint "ix_tb_oferta_vaga"
Detalhe: Key (cd_pj, cd_curso)=(680, 29) already exists.
Run Code Online (Sandbox Code Playgroud)
getMessage():
could not insert: [br.gov.ce.seduc.estagio.model.bean.OfertaVaga]; nested exception is org.hibernate.exception.ConstraintViolationException: could not insert: [br.gov.ce.seduc.estagio.model.bean.OfertaVaga]
Run Code Online (Sandbox Code Playgroud)
谢谢.
亚瑟
小智 8
插入如下catch语句:
catch (DataIntegrityViolationException e) {
String message = e.getMostSpecificCause().getMessage();
}
Run Code Online (Sandbox Code Playgroud)
包装异常通常有一种方法可以将原始异常嵌套在其中。对于 Hibernate,您的 ConstraintViolationException 是一个 JDBCException,它有一个名为getSQLException的方法,它返回实际的异常。因此,在 Spring DataIntegrityViolationException 上调用 getCause(为了获得 Hibernate 异常),在其上调用 getSQLException,最后在 SQLException 上调用 getMessage()。该消息应该与您看到的 Hibernate JDBCExceptionReporter 记录的内容相同,如果您只需要约束名称,则必须解析字符串。
| 归档时间: |
|
| 查看次数: |
4274 次 |
| 最近记录: |