加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何在org.springframework.dao.DataIntegrityViolationExceptio

发布时间:2020-12-15 01:49:35 所属栏目:大数据 来源:网络整理
导读:在我的应用程序中,当提出违规密钥时,我想获得约束名称,但我找不到任何获取此信息的方法. “getMessage()”返回的消息非常概括,我需要有关错误的更多信息,以便向最终用户发出可自定义的错误消息. 堆栈跟踪: 84732 [http-8080-1] WARN org.hibernate.util.JDB

在我的应用程序中,当提出违规密钥时,我想获得约束名称,但我找不到任何获取此信息的方法. “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中看到的消息相同,如果您只需要解析字符串的约束名称.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读