Reputation: 1539
I have my application in Spring / JPA. Spring translate database exception into Runtime exception called DataAccessException. I have unique constraint on one of my column and I want to display user defined message that Value for this column already exists. But when I retrieve message using e.getMessage();
it just give me Could not execute JDBC batch update. Which is insufficient.
Can I get any database specific error code the way database vendor returns so that I can do some mappping based on that.
spring does provide sql-error-code.xml using which we can translate. But I am using jpatemplate and not jdbcTemplate. Please help me as I am new to exception handling in spring.
Thanks in advance.
Upvotes: 2
Views: 756
Reputation: 62632
Quoting the spring manual:
Spring provides a convenient translation from technology-specific exceptions like SQLException to its own exception class hierarchy with the DataAccessException as the root exception. These exceptions wrap the original exception so there is never any risk that one might lose any information as to what might have gone wrong.
You should catch the Spring DataIntegrityViolationException and then set whatever use message makes sense for your user.
Full list of exceptions here http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/dao.html
Upvotes: 1