title: Hibernate : Stale state exception
body: <p>I am working on a Spring-MVC application in which I am trying to delete an object from the database. Some days back, this error all of a sudden started, and now I am unable to delete. I checked on net, but I cannot find what am I doing wrong and none of the solutions seem to work.</p>
  
  <p>Service layer code :</p>
  
  <pre><code>  @Override
      public boolean deleteGroupSection(int sectionId, int mcanvasId) {
          try {
          GroupCanvas groupCanvas = this.groupCanvasService.getCanvasById(mcanvasId);
          Long groupAccountId = this.groupAccountService.getGroupById(groupCanvas.getGroupAccountId()).getGroupId();
              this.groupAttachmentsService.deleteAttachmentsForSection(sectionId,groupAccountId);
          }catch (Exception ignored){}
          this.groupSectionDAO.deleteGroupSection(sectionId,mcanvasId);
          return true;
      }
  </code></pre>
  
  <p>DAO code :</p>
  
  <pre><code> @Override
      public boolean deleteGroupSection(int sectionid, int mcanvasId) {
          Session  session = this.sessionFactory.getCurrentSession();
           GroupSection groupSection = (GroupSection) session.get(GroupSection.class,sectionid);
           session.delete(groupSection);
           session.flush();
           return true;
      }
  </code></pre>
  
  <p>Error code :</p>
  
  <pre><code>SEVERE: Servlet.service() for servlet [appServlet] in context with path [] threw exception [Request processing failed; nested exception is org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1] with root cause
  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:81)
      at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:73)
      at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:63)
      at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3400)
      at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3630)
      at org.hibernate.action.internal.EntityDeleteAction.execute(EntityDeleteAction.java:114)
      at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:463)
      at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:349)
      at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
      at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
      at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222)
      at com.journaldev.spring.dao.GroupSectionDAOImpl.deleteGroupSection(GroupSectionDAOImpl.java:119)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:497)
  </code></pre>
  
  <p>What am I doing wrong. Any help would be nice. Thanks a lot. :-)</p>