在我的应用程序中,当出现违规键时,我想获取约束名称,但我找不到任何方法来获取此信息。 “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.
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]
谢谢。
亚瑟
最佳答案
像这样插入一个catch
语句:
catch (DataIntegrityViolationException e) {
String message = e.getMostSpecificCause().getMessage();
}
关于java - 如何在 org.springframework.dao.DataIntegrityViolationException 中获取约束名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7111134/