title: Roll back transaction after exception in JPA + Spring
body: <p>I'm using Spring and JPA with HIbernate underneath. When a PersistenceException is thrown, I want to catch it and return the error message so that it is not propagated up to the caller.</p>
  
  <pre><code>@Transactional
  public String save(Object bean) {
      String error = null;
  
      try {
          EntityManager entityManager = getEntityManager();
  
          for (int i = 0, n = entities.size(); i &lt; n; i ++) {
              entityManager.merge(entities.get(i));
          }
      }
      catch (PersistenceException e) {
          error = e.getMessage();
      }
  
      return error;
  }
  </code></pre>
  
  <p>But I get an exception saying that <code>javax.persistence.RollbackException: Transaction marked as rollbackOnly.</code> I get that the transaction needs to be rolled back after an exception but how do I roll it back when I've catched the exception and do not want to re-throw it?</p>