2011-08-18 13 views
4

내 응용 프로그램에서 위반 키가 발생하면 제약 조건 이름을 가져오고 싶지만이 정보를 얻는 방법을 찾지 못하고 있습니다. "getMessage()"에 의해 반환 된 메시지는 매우 요약되어 있으며 최종 사용자에게 사용자 정의 가능한 오류 메시지를 만들기 위해 오류에 대한 추가 정보가 필요합니다.org.springframework.dao.DataIntegrityViolationException에서 제약 조건 이름을 얻는 방법은 무엇입니까?

스택 추적 :

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] 

감사합니다.

아서

답변

2

포장 예외는 일반적으로 둥지 방법 그 안에 원의 예외가 있습니다. Hibernate의 경우 ConstraintViolationException은 JDBCException이며 실제 예외를 리턴하는 getSQLException이라는 메소드가 있습니다. 그러므로 Spring DataIntegrityViolationException에 대해 getCause (Hibernate 예외를 얻기 위해)를 호출하고, getSQLException을 호출하고 마지막으로 SQLException에 대해 getMessage()를 호출한다. 이 메시지는 Hibernate JDBCExceptionReporter에 의해 로그 된 것과 같아야한다. 제약 조건 이름 만 원한다면 문자열을 파싱해야한다.

3

이 같은 catch 문 삽입 :이 형식의 문자열을 반환합니다

catch (DataIntegrityViolationException e) { 
     String message = e.getMostSpecificCause().getMessage(); 
} 
+1

을'ERROR : 키 (ORGANIZATION_NAME) = (빌라 대학 QI 캠퍼스 : 중복 키 값 상세 "uk_meb3tm159kt0clyot5mmv8oht"고유 제한 조건을 위반) 이미 존재한다. 최종 사용자에게 메시지를 제공하려는 경우 유용하지 않습니다. –