title: Exception Handling in DAO layer of Spring Project with JPA
body: <p>I am using Spring &amp; JPA with Hiberate as vendor. </p>
  
  <p>In DAO layer I am trying to persist entity. If duplicate exists, it throws ConstraintViolationException of Hibernate. I have written try-catch in DAO for catching the exception but it does't go to catch block at all and throws Exception to service layer. 
  Does JPA allow to catch JDBCException in DAO layer or it'll directly throw it to upper layer?</p>
  
  <p>My code looks like as follows :</p>
  
  <pre><code>public void saveEntity(SomeEntity entity) throws CustomException {
     try {
        ... do something...
        entityManager.persist(entity);
      }catch(Exception e) {
        throw new CustomException(e.getMessage());
      }
  }
  </code></pre>
  
  <p>Here If something happens before persist it catches but if something goes wrong while persisting and JDBCException comes it doesn't.</p>
  
  <p>I know that I could have avoided this situation by checking first that if record exists and if not, only then I'll save. But I want to know why JDBCException (or any database related exceptions) exceptions are not getting caught here.</p>
  
  <p>Any help appreciated.</p>