0

How to handle org.hibernate.exception.ConstraintViolationException? I am using try-catch, but it doesn't work:

try {
    CandidateCollege newCandidateCollege = candidateCollegeRepo.save(candidateCollege);
    System.out.println(newCandidateCollege);
    if(newCandidateCollege != null){
        return new WithIncludes<>(newCandidateCollege);
    }
} catch (Exception ex) {
    System.out.println("==========ex.getMessage()============");
    log.error(ex.getMessage());
}
return null;

Exception:

2017-05-25 15:52:43 ERROR SqlExceptionHelper:131 - Duplicate entry '270-1-4-2' for key 'ux_candidate_college'
==========ex.getMessage()============
2017-05-25 15:52:43 ERROR CandidateCollegeServiceImpl:67 - could not execute statement; SQL [n/a]; constraint [ux_candidate_college]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
1
  • Well, don't try saving a duplicate entry. And if you really want to catch that exception, read its stack trace: you'll see that it happens at flush time, not when calling save(), because flush is when Hibernate actually executes the inserts and updates. So, call flush explicitely inside your try block, or put the try block at a higher level, outside of the transaction. Commented May 25, 2017 at 8:07

3 Answers 3

6

It wont catch your Exception because the ConstraintViolationException is being thrown at the time of transaction flush which is actually not happening between your try-catch block or during save() call.

you can surround the try-catch at some higher level like eg. in the Controller and you could check for the ConstraintViolationException is thrown and take action on accord.

or can flush your transaction explicitly after save()

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks , very helpful!
Hi @GreenBaylee I am also facing something similar. And my try catch is not able to handle the exception as method is annotated with Transactional. Could you please tell me the approach you used to fix this problem if you remember. Thanks in advance.
@Govind Use try catch where you are calling your transactional method.
0

Looking at the output, I would say that the catch block is executed. Note the ===== in the output. The message above that is just Hibernate's internal logging. I would expect that you method does indeed return null. Have you checked that?

Comments

0

Before saving that entry in the db, you can check if it already exists or not, if not then you can go with saving the entry. Also specify
catch(ConstraintViolationException cve){ //some appropriate thing to deal with the exception }

this way you also get to handle the specific exception case that you are looking for!

1 Comment

May be it wouldnt be a better idea to check the existence before saving cause it will cost another round trip to the database. Handling exception at correct point would be performance healthy @coolgirl

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.