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

“Incoming Call” Activity may not be killed on Home Press
What is Android's SharedPreferences equivalent in Java?
how to mark avro field deprecated in JSON/avsc?
How do I keep my Android app running when it doesn't have focus?
Any easier way to construct dynamic deep nested json objects in Java
Change 6.51 to 6.50 in java BigDecimal
Auto delete database rows after an expiration date
I try to use the method repaint after paintComponent but its not work
Defining an equation outside of function
TransformException: java.util.zip.ZipException: duplicate entry: com/google/gson/JsonPrimitive.class
When given a pair of ints, I need my code to be able to return the int that is closest to 21 without going over [closed]
Spring Data MongoDB: Unit tests with Fongo
Tomcat and vagrant: how to get the same result as using war file
Java MongoDB: What is the difference between com.mongodb.DB and com.mongodb.client
JUnit and Mockito Null Pointer Exception [duplicate]
C++ Arduino How can I initialize anything an uninitialized global variable in a method?

Categories

HOME
minimum-spanning-tree
tizen
google-play
angular-material
json.net
relative-path
office365api
electron
react-router
sqlite-net-extensions
yahoo-oauth
ebean
flyway4
here-api
user-input
tostring
openedx
language-agnostic
wijmo
autosys
fish
assistant
saas
phonegap
preg-match-all
code-contracts
calibre
subset-sum
sequential
wpf-controls
wixsharp
io-redirection
file-format
libvpx
sharefile
azure-ml
eclipse-scout
mime
heightmap
qcombobox
wso2carbon
angular-resource
fault
dotcover
komodoedit
multipeer-connectivity
paxos
darcs
carrot
sqlclient
nodebb
typed-lambda-calculus
np-complete
objective-c-swift-bridge
setuptools
rotativa
elements
login-control
merge-conflict-resolution
qpid
holder.js
asp.net5
independentsoft
tcpserver
xna-4.0
msys
pundit
arcanist
apache-commons-fileupload
mmc
inmobi
lustre
system.reflection
offloading
ng-animate
codeigniter-routing
asp.net-web-api-odata
onactivityresult
terminfo
uitouch
funcunit
jquery-layout
monomac
tidy
sabredav
isnullorempty
trusted
quickdialog
propertyeditor
jspinclude
wse3.0
locationlistener
servicehost
actionview
pyinotify
cxxtest
google-friend-connect
subviews
digest-authentication
getresponsestream
firefox-5
nerddinner

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App