title: JPA, Hibernate - handling ContraintViolationException wrapped into RollbackException
body: <p>I'm having problem processing <code>ConstraintViolationException</code> and <code>RollbackException</code> in JPA2 / Hibernate / Guice environment. Both exceptions occur while bean validation, but:</p>
  
  <p><code>ConstraintViolationException</code> is thrown when I try to <strong>persist</strong> a new entity</p>
  
  <p><code>RollbackException</code> occurs when I'm trying to <strong>merge</strong> the entity into context</p>
  
  <p>This happens while form processing (both creating and updating entities) and catching these exceptions is tiresome:</p>
  
  <pre><code>try {
      // service.method is annotated with @Transactional
      entity = service.create(formEntity);
  } catch (RollbackException re) {
      // occurs while merging the entity
      if (re.getCause() instanceof ConstraintViolationException) {
          errors.process((ConstraintViolationException) re.getCause());
      }
  } catch (ConstraintViolationException cve) {
      // occurs while persisting the entity
      errors.process(cve);
  }
  </code></pre>
  
  <p>I would not like to add another control flow and catching all <code>RuntimeException</code>s:</p>
  
  <pre><code>try {
      // bean validation fails (merge / persist)
  } catch (RuntimeException ex) {
      if (errors.process(ex)) {
          // do something with entity
      }
  }
  </code></pre>
  
  <p>Is it possible to somehow force hibernate to not wrap CVE into RE? What is the most transparent and DRY way of processing and handling such exceptions?</p>
  
  <p>Thanks</p>