title: exception in hibernate program
body: <p>I am following a basic hibernate tutorial. The tutorial program is running &amp; showing output as described in the tutorial but it is showing exception after the output. The program writes, retrieves, &amp; deletes values in/from two MySql database.<br>
  Here is the exception:</p>
  
  <pre><code>29088 [main] ERROR org.hibernate.jdbc.AbstractBatcher - Exception executing batch: 
  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:85)
      at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:70)
      at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:90)
      at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
      at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
      at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
      at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:172)
      at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
      at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
      at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
      at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
      at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
      at com.hib.Test.deleteUser(Test.java:187)
      at com.hib.Test.main(Test.java:55)
  Hibernate: delete from users where id=?
  29093 [main] ERROR org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session
  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:85)
      at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:70)
      at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:90)
      at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
      at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
      at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
      at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:172)
      at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
      at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
      at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
      at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
      at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
      at com.hib.Test.deleteUser(Test.java:187)
      at com.hib.Test.main(Test.java:55)
  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:85)
      at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:70)
      at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:90)
      at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
      at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
      at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
      at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:172)
      at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
      at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
      at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
      at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
      at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
      at com.hib.Test.deleteUser(Test.java:187)
      at com.hib.Test.main(Test.java:55)
  </code></pre>
  
  <p>This is main program.</p>
  
  <pre><code>package com.hib;
  
  import java.util.Iterator;
  import java.util.List;
  import org.hibernate.Session;
  import org.hibernate.Transaction;
  
  public class Test {
  
   /**
    * @param args
    */
   public static void main(String[] args) {
  
    Test tst = new Test();
  
    /**
     * adding records
     */
    tst.addUser("Saranga", "Rath");
    tst.addUser("Isuru", "Sampath");
    tst.addUser("Saranga", "Jaya");
    tst.addUser("Prasanna", "Milinda");
  
    tst.addTask(1, "Call", "Call Pubudu at 5 PM");
    tst.addTask(1, "Shopping", "Buy some foods for Kity");
    tst.addTask(2, "Email", "Send birthday wish to Pubudu");
    tst.addTask(2, "SMS", "Send message to Dad");
    tst.addTask(2, "Office", "Give a call to Boss");
  
    /**
     *  retrieving data
     */
    tst.getFullName("Saranga");
  
    /**
     * full updating records
     */
    User user = new User();
    user.setId(1);
    user.setFirstName("Saranga");
    user.setLastName("Rathnayake");
    tst.updateUser(user);
  
    /**
     * partial updating records
     */
    tst.updateLastName(3, "Jayamaha");
  
    /**
     * deleting records
     */
    User user1 = new User();
    user1.setId(4);
    tst.deleteUser(user1);
   }
  
   private void addUser(String firstName, String lastName) {
  
    Transaction trns = null;
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {
     trns = session.beginTransaction();
  
     User user = new User();
  
     user.setFirstName(firstName);
     user.setLastName(lastName);
  
     session.save(user);
  
     session.getTransaction().commit();
    } catch (RuntimeException e) {
     if(trns != null){
      trns.rollback();
     }
     e.printStackTrace();
    } finally{
     session.flush();
     session.close();
    }
   }
  
   private void addTask(int userID, String title, String description) {
  
    Transaction trns = null;
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {
     trns = session.beginTransaction();
  
     Task task = new Task();
  
     task.setUserID(userID);
     task.setTitle(title);
     task.setDescription(description);
  
     session.save(task);
  
     session.getTransaction().commit();
    } catch (RuntimeException e) {
     if(trns != null){
      trns.rollback();
     }
     e.printStackTrace();
    } finally{
     session.flush();
     session.close();
    }
   }
  
   private void updateLastName(int id, String lastName) {
    Transaction trns = null;
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {
     trns = session.beginTransaction();
     String hqlUpdate = "update User u set u.lastName = :newLastName where u.id = :oldId";
     int updatedEntities = session.createQuery( hqlUpdate )
     .setString( "newLastName", lastName )
     .setInteger( "oldId", id )
     .executeUpdate();
  
     trns.commit();
    } catch (RuntimeException e) {
     if(trns != null){
      trns.rollback();
     }
     e.printStackTrace();
    } finally{
     session.flush();
     session.close();
    }
  
   }
  
   private void updateUser(User user) {
    Transaction trns = null;
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {
     trns = session.beginTransaction();
  
     session.update(user);
  
     session.getTransaction().commit();
    } catch (RuntimeException e) {
     if(trns != null){
      trns.rollback();
     }
     e.printStackTrace();
    } finally{
     session.flush();
     session.close();
    }
   }
  
   private void getFullName(String firstName) {
    Transaction trns = null;
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {
     trns = session.beginTransaction();
     List&lt;User&gt; users = session.createQuery("from User as u where u.firstName = :firstName")
     .setString( "firstName", firstName )
     .list();
     for (Iterator&lt;User&gt; iter = users.iterator(); iter.hasNext();) {
      User user = iter.next();
      System.out.println(user.getFirstName() +" " + user.getLastName());
     }
     trns.commit();
    } catch (RuntimeException e) {
     if(trns != null){
      trns.rollback();
     }
     e.printStackTrace();
    } finally{
     session.flush();
     session.close();
    }
   }
  
   private void deleteUser(User user) {
    Transaction trns = null;
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {
     trns = session.beginTransaction();
  
     session.delete(user);
  
     session.getTransaction().commit();
    } catch (RuntimeException e) {
     if(trns != null){
      trns.rollback();
     }
     e.printStackTrace();
    } finally{
     session.flush();
     session.close();
    }
   }
  }
  </code></pre>
  
  <p>HibernateUtil.java</p>
  
  <pre><code>package com.hib;
  
  import org.hibernate.SessionFactory;
  import org.hibernate.cfg.Configuration;
  
  public class HibernateUtil {
   private static final SessionFactory sessionFactory = buildSessionFactory();
   private static SessionFactory buildSessionFactory() {
    try {
     // Create the SessionFactory from hibernate.cfg.xml
     return new Configuration().configure().buildSessionFactory();
    }
    catch (Throwable ex) {
     // Make sure you log the exception, as it might be swallowed
     System.err.println("Initial SessionFactory creation failed." + ex);
     throw new ExceptionInInitializerError(ex);
    }
   }
   public static SessionFactory getSessionFactory() {
    return sessionFactory;
   }
  }
  </code></pre>
  
  <p>User.java</p>
  
  <pre><code>package com.hib;
  
  public class User {
   private Integer id;
   private String firstName;
   private String lastName;
  
   public Integer getId() {
    return id;
   }
   public void setId(Integer id) {
    this.id = id;
   }
   public String getFirstName() {
    return firstName;
   }
   public void setFirstName(String firstName) {
    this.firstName = firstName;
   }
   public String getLastName() {
    return lastName;
   }
   public void setLastName(String lastName) {
    this.lastName = lastName;
   }
  }
  </code></pre>
  
  <p>Task.java</p>
  
  <pre><code>package com.hib;
  
  public class Task {
  
   private Integer id;
   private Integer userID;
   private String title;
   private String description;
  
   public Integer getId() {
    return id;
   }
   public void setId(Integer id) {
    this.id = id;
   }
   public Integer getUserID() {
    return userID;
   }
   public void setUserID(Integer userID) {
    this.userID = userID;
   }
   public String getTitle() {
    return title;
   }
   public void setTitle(String title) {
    this.title = title;
   }
   public String getDescription() {
    return description;
   }
   public void setDescription(String description) {
    this.description = description;
   }
  }
  </code></pre>