title: How to handle org.hibernate.StaleStateException?
body: <p>I use REST APIs  which talk to database.</p>
  
  <p>Using Hibernate with Wildfly 10.1.0</p>
  
  <p>When I do concurrent update I get following exception;</p>
  
  <p>What is the proper solution for this?
  Should I send any error code to the client or should I handle at the backend?</p>
  
  <pre><code>Caused by: org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
      at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:67)
  </code></pre>
  
  <p><strong>Edit:</strong></p>
  
  <pre><code>public void saveOrUpdate(T entity) {
          Session session = null;
          try {
               session = getSession();
               session.clear();
               session.saveOrUpdate(entity);
               session.flush();
  
          } catch (HibernateException ex) {
              log.error("Error when save or update." + entity.getCode(), ex.getLocalizedMessage());
              throw new HibernateException(ex);
          }
      }
  </code></pre>