Handling Duplicate Grails Duplicate
How to catch duplicate key exceptions in Grails. when trying to store an existing integer for a unique column constraint, an error is generated when saving / updating the record.
Also used
try{
object.save(flush:true)
}
catch(org.springframework.dao.DataIntegrityViolationException e){
println e.message
}
catch(org.hibernate.exception.ConstraintViolationException ex){
println e.message
}
catch(Exception e){
println e.message
}
But failed to catch this issue.
23: 41: 13.265 ERROR [JDBCExceptionReporter: 101] Duplicate record '1' for key 2
23: 41: 13.281 ERROR [AbstractFlushingEventListener: 324] Failed to sync database
with session org.hibernate.exception.ConstraintViolationException: Failed to do JDBC batch update in org.hibernate.exception.SQLStateConverter.convert (SQLStateConverter.java:94) in org.hibernate.exception.JDBCExceptionHelper.convert (JDBCExavaceptionH66) at org.hibernate.jdbc.AbstractBatcher.executeBatch (AbstractBatcher.java:275) at org.hibernate.engine.ActionQueue.executeActions (ActionQueue.java:266) at org.hibernate.engine.ActionQueue.executeActions (Action168ueue.java ) at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions (AbstractFlushingEventListener.java:321) at org.hibernate.event.def.DefaultFlushEventListener.onFlush (DefaultFlushEventListener.javah:50) at org. SessionImpl.java:1027)
Could you please suggest a solution to solve this problem.
a source to share
If you've determined uniqueness through a Grails constraint, then you need to look for a ValidationException. This is thrown when object.validate () fails; The check is done before any .save () object.
try {
object.save(failOnError: true)
}
catch(ValidationException ve) {
// Do something ...
}
But remember: any constraint violation on any member variable can throw a ValidationException ... so you need to differentiate yourself.
Edit:
This is applicable if you are using Grail 1.2 failOnError ...
a source to share
I'm looking for the same problem, so maybe not a complete answer, but what you can do is force check and see errors, identify the case, and put the actions you want:
if(instance.validate()) {
//everything ok
} else {
instance.errors.each {
//identify the case and place actions
}
}
Also note that the error is: className.propertyName.unique
a source to share