如何在org.springframework.dao.DataIntegrityViolationException中获取约束名称?

前端之家收集整理的这篇文章主要介绍了如何在org.springframework.dao.DataIntegrityViolationException中获取约束名称?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在我的应用程序中,当提出违规密钥时,我想获得约束名称,但我找不到任何获取此信息的方法. “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,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]

谢谢.

亚瑟

最佳答案
包装异常通常可以将原始异常嵌套在其中.对于Hibernate,您的ConstraintViolationException是JDBCException,它有一个名为getSQLException方法,它返回实际的异常.因此,在Spring DataIntegrityViolationException上调用getCause(为了获得Hibernate异常),在其上调用getsqlException,最后在sqlException上调用getMessage().该消息应与您在Hibernate JDBCExceptionReporter中看到的消息相同,如果您只需要解析字符串的约束名称.
原文链接:https://www.f2er.com/spring/432757.html

猜你在找的Spring相关文章