title: org.hibernate.AssertionFailure
body: <p>I am getting this strange error sometimes while my thread executes. What could this be related to?</p>
  
  <pre><code>2011-Jun-25 09:05:22,339 ERROR AssertionFailure:45 - an assertion failure occured (this             may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)
  org.hibernate.AssertionFailure: null id in com.inrev.bm.bean.IRKeyWordTweet entry (don't flush the Session after an exception occurs)
      at         org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:78)
  at org.hibernate.event.def.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:187)
  at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:143)
  at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:219)
  at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
  at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:49)
  at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1028)
  at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:366)
  at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
  at com.inrev.bm.streaming.IRKeyWordStreaminThread.run(IRKeyWordStreaminThread.java:119)
  </code></pre>
  
  <p>My code for inserting,</p>
  
  <pre><code>    Transaction tx = null;
      Session session = sessionFactory.openSession();
      tx = session.beginTransaction();
  
      int count = 0;
      try
      {
          for (Iterator itrList = statusToInsert.iterator(); itrList.hasNext();) 
          {
              try 
              {
                  IRecord record = (IRecord) itrList.next();
                  session.save(record);
                  count++;
                  if ( count % 10 == 0 ) 
                  { 
                      session.flush();
                      session.clear();
                      tx.commit();
                      tx = session.beginTransaction();
                  }
  
              }
              catch (Exception e) 
              {
                  tx.commit();
                  session.close();
                  session = sessionFactory.openSession();
                  tx = session.beginTransaction();
                  StringWriter sw = new StringWriter(); 
                  PrintWriter pw = new PrintWriter(sw); 
                  e.printStackTrace(pw); 
                  log.error(sw.toString());
              }
          }
      }
      catch (Exception e) {
          StringWriter sw = new StringWriter(); 
          PrintWriter pw = new PrintWriter(sw); 
          e.printStackTrace(pw); 
          log.error(sw.toString());
      }
      finally {
          tx.commit();
          session.close();
      }
  </code></pre>
  
  <p>Regards,</p>
  
  <p>Rohit</p>