title: Hibernate and/or JDBC batch insert/update exception handling
body: <p>Suppose I am inserting 100 rows in a batch operation in Hibernate, similar to:</p>
  
  <pre><code>beginTx with flush mode manual;
  
  insert 100 rows;
  do 100 times {
      insert row
      commit;
  }
  flush;
  clear;
  </code></pre>
  
  <p>Ideally, this should translate into a batch operation (since batch-size param, keys set by Hibernate, etc).
  But suppose one of the inserts in the batch fail on FK constraint, or whatever.
  Individual rows were committed, but the problem (constraint violation) only during flush.</p>
  
  <p>Is this scenario possible? (I was 100% sure this is what happens here until now)
  If so, should I worry about it, or does JDBC or Hibernate take care of business?
  What's the best approach to handle this?</p>
  
  <p>Thanks.</p>
  
  <p>-- Edit: Here's the exception    </p>
  
  <pre><code>ERROR Caught exception while trying to flush: 
  org.hibernate.exception.ConstraintViolationException: could not execute batch
      at    org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:129)
  at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
  at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
  at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.performExecution(BatchingBatch.java:132)
  at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.doExecuteBatch(BatchingBatch.java:111)
  at org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl.execute(AbstractBatchImpl.java:163)
  at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.executeBatch(JdbcCoordinatorImpl.java:217)
  at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:472)
  at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:345)
  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:1218)
  ...
  
  Caused by: java.sql.BatchUpdateException: Duplicate entry '218947-449145434175721472' for key 'Unique XXX AAA and BBB'
      ...   
  
  Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '218947-449145434175721472' for key 'Unique XXX AAA and BBB'
  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  </code></pre>