title: Java. Hibernate. org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
body: <p>I'm new to Hibernate.
  Now working on the example from web.
  I have a MySQL database that contains some tables, including table "profession". This table has two cols: profession_id(auto-increment) and profession_name.</p>
  
  <p>In java project I have a 'Test' class, that contains several methods for table processing and 'main' method. 
  Processing methods are:</p>
  
  <pre><code>private void addProfession(String name) {
      Session session = HibernateUtil.getSessionFactory().getCurrentSession();
      session.beginTransaction();
      Profession r = new Profession();
      r.setProfessionName(name);
      session.save(r);
      session.getTransaction().commit();
  }
  
  private ArrayList&lt;Profession&gt; getProfessions() {
      Session session = HibernateUtil.getSessionFactory().getCurrentSession();
      session.beginTransaction();
      ArrayList&lt;Profession&gt; result = (ArrayList&lt;Profession&gt;) session
              .createQuery("from Profession order by professionName").list();
      session.getTransaction().commit();
      return result;
  }
  
  private void deleteProfessions(ArrayList&lt;Profession&gt; result) {
      Session session = HibernateUtil.getSessionFactory().getCurrentSession();
      session.beginTransaction();
      for (Profession p : result) {
          System.out.println("Delete: " + p.getProfessionId() + ": " + p.getProfessionName());
          session.delete(p);
      }
      session.getTransaction().commit();
  }
  
  private void deleteEntity(Object object){
      Session session = HibernateUtil.getSessionFactory().getCurrentSession();
      session.beginTransaction();
      session.delete(object);
      session.flush();
  
      session.getTransaction().commit();
  
  }
  </code></pre>
  
  <p>'main' method contains these operations:</p>
  
  <pre><code>public static void main(String[] args) {
      Test test = new Test();
      test.addProfession("Profession_1");
      test.addProfession("Profession_2");
      test.addProfession("Profession_3");
      test.addProfession("Profession_4");
      test.addProfession("Profession_5");
  
      ArrayList&lt;Profession&gt; result = test.getProfessions();
  
      test.deleteEntity(result.get(0));
      test.deleteProfessions(result);
  }
  </code></pre>
  
  <p>So, the problems is: when I run it I get </p>
  
  <pre><code>Exception in thread "main" 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)
  at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:54)
  at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:46)
  at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3197)
  at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3434)
  at org.hibernate.action.internal.EntityDeleteAction.execute(EntityDeleteAction.java:98)
  at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:560)
  at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:434)
  at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:337)
  at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
  at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1282)
  at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:465)
  at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:2963)
  at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2339)
  at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:485)
  at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:147)
  at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$100(JdbcResourceLocalTransactionCoordinatorImpl.java:38)
  at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:231)
  at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:65)
  at firstproject.Main.deleteProfessions(Main.java:33)
  at firstproject.Main.main(Main.java:57)
  </code></pre>
  
  <p>After reading several topics about this theme at stackoverflow I learned, that this problem somehow is connected with ids. So I decided to comment this code:</p>
  
  <pre><code>test.deleteEntity(result.get(0));
  </code></pre>
  
  <p>After that everything worked fine.</p>
  
  <p>So the question is:
  How do I avoid this exception if i need: 
  1. Delete one of the rows('cos this problem also occurs when I delete for example result.get(3) and so on).
  2. Delete remaining rows.</p>