java


How to get the constraint name in a org.springframework.dao.DataIntegrityViolationException?


In my application when a violation key is raised, I would like to get the constraint name, but I'm not finding any method to get this information. The message returned by "getMessage()" is very summarized and I need more information about the error to make a customizable error message to the end user.
The stack trace:
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.
The 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]
Thanks.
Arthur
Wrapping exceptions usually have a way to nest the original exception within them. For Hibernate, your ConstraintViolationException is a JDBCException, it has a method called getSQLException that returns the actual exception. So call getCause on the Spring DataIntegrityViolationException (in order to get the Hibernate exception), call getSQLException on that, and finally call getMessage() on the SQLException. The message should be the same as what you see logged by the Hibernate JDBCExceptionReporter, if you want only the constraint name you will have to parse the string.
Insert a catch statement like this:
catch (DataIntegrityViolationException e) {
String message = e.getMostSpecificCause().getMessage();
}

Related Links

Converting stream of int's to char's in java
Auto-wiring a List using util schema gives NoSuchBeanDefinitionException
Getting the Time component of a Java Date or Calendar
Java Logging framework with parsers generated?
Is there absolutely NO way in Java to pass the object itself to a method so it actually gets changed?
Retrieve an ArrayListMultimap Key
Is it possible to write swap method in Java? [duplicate]
Java VMs that do not require an operating system?
Validating instances of inner-classes
Setting JVM heap size at runtime
Declarative derived properties for mutable models in Java
How to use #BasicMap for a HashMap whose key values are entities
Unexpected multithreaded result
How do I get a simple, Hello World Java applet to work in a browser in Mac OS X?
The best way to store and access 120,000 words in java
Killing a windows session from a Java program

Categories

HOME
testing
cookies
reserved
iterator
fme
gis
rsync
leon
infragistics
amazon-ecs
swagger-ui
synchronization
basic
applepay
quicklook
handsontable
visual-studio-2005
zebra-printers
quickfix
carthage
serverless-framework
fatal-error
pugjs
nhibernate-envers
plunker
restful-authentication
spring-mybatis
elasticsearch-ruby
vision
webtest
commit
asset-pipeline
rotational-matrices
environment-modules
hot-module-replacement
event-driven
android-browser
service-discovery
vapor
sas-visual-analytics
preconditions
optix
reportingservices-2005
s
jquery-validate
promela
pearson
dynamics-sl
ado.net-entity-data-model
thrust
impresspages
make-install
packagist
nessus
php-internals
hsv
citrus-pay
kcachegrind
ideamart
bigbluebutton
nsmutabledictionary
codeigniter-url
tld
reactfx
valuechangelistener
cloud-connect
spring-io
ember-app-kit
quickdialog
ocx
amazon-appstore
cinema-4d
rfc1123
javax.script
disclosure
inotifycollectionchanged
jmock
telerik-scheduler
cxxtest
yslow
asp.net-mvc-areas
bespin
brewmp
ti-dsp
ctp4
swing-app-framework
avatar
data-acquisition
3gp

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App