title: JPA try to delete if fails set inactive
body: <p>i have the following entity A (imagine B as just a simple enitity with an active/inactive-flag which can be referenced by other entities)</p>
  
  <pre><code>@Entity
  public class A { 
      @OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
      private B b;
  }
  </code></pre>
  
  <p>i need to try to remove b, but if it is referenced by other entities i want to fall back to inactivating it. </p>
  
  <p>imagine something like this</p>
  
  <pre><code>public void removeB() {
      B toRemove = this.b;
      this.b = null;
      try {
          entityManager.remove(toRemove);
          entityManager.flush();
      } catch (ConstraintViolationException e) {
          toRemove.setActive(false)
      }
  }
  </code></pre>
  
  <p>the problem is that this gives me the error</p>
  
  <pre><code>Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly
  </code></pre>
  
  <p>Any idea how i can achive this?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>my problem is not about showing error messages to the user, for that i found library that could do that</p>
  
  <p><a href="https://blog.42.nl/articles/recovering-from-database-constraint-violations-in-java/" rel="nofollow">https://blog.42.nl/articles/recovering-from-database-constraint-violations-in-java/</a></p>
  
  <p>my problem is about recovering from any exception that might occur in a delete</p>