Spring Data Exception Handling

java, spring, spring-data, spring-data-jpa

Solution

Wherever you handle the exception, you have the option of looking into the `getMostSpecificCause()` or `getRootCause()` methods of `UnexpectedRollbackException`. Here is information about those methods.

Problem

I´m working on a project using Spring Data-JPA. I need to handle some exceptions in JpaRepository method calls. In the code bellow, I need to intercept primary key violations erros but I cannot catch the exception directly. In my case, when an exception of this kind occurs, the UnexpectedRollbackException exception is thrown by repository layer (JpaRepository). I need to search inside this exception object to determine what is the cause of the problem. I am wondering if there is a more "elegant" way to achieve this. ``` public Phone insert(Phone phone) throws BusinessException { Phone result = null; try{ result = phoneRepository.save(phone); } catch(UnexpectedRollbackException ex){ if((ex.getCause() != null && ex.getCause() instanceof RollbackException) && (ex.getCause().getCause() != null && ex.getCause().getCause() instanceof PersistenceException) && (ex.getCause().getCause().getCause() != null && ex.getCause().getCause().getCause() instanceof ConstraintViolationException)){ throw new BusinessException("constraint violation", ex); } } catch(Exception ex){ throw new OuvidorNegocioException("unknown error", ex); } return result; } ``` Thanks! UPDATE: The code bellow seems to be much better. ``` public Phone insert(Phone phone) throws BusinessException { Phone result = null; try{ result = phoneRepository.save(phone); } catch(UnexpectedRollbackException ex){ if(ex.getMostSpecificCause() instanceof SQLIntegrityConstraintViolationException){ throw new BusinessException("constraint violation", ex); } } catch(Exception ex){ throw new OuvidorNegocioException("unknown error", ex); } return result; } ```

Original source