title: JPA + Hibernate : org.hibernate.exception.ConstraintViolationException: could not execute batch
body: <p>I wanted to use batch insertion / update provided by hibernate(using MySQL 5.7) as given in <a href="https://www.baeldung.com/jpa-hibernate-batch-insert-update" rel="nofollow noreferrer">this</a>.<br>
  I got to know that we cannot used batch inserts / updates for entities having id generation as IDENTITY.<br>
  More info: <a href="https://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/ch15.html" rel="nofollow noreferrer">Hibernate disables insert batching at the JDBC level transparently if you use an IDENTITY identifier generator.</a>
  <br>So, I changed <code>@GeneratedValue(strategy = GenerationType.IDENTITY)</code> to <code>@GeneratedValue(strategy = GenerationType.SEQUENCE)</code> for one of the hibernate entity and started getting the below error.</p>
  <pre><code>javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute batch
      at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:149) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
      at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:157) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
      at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:164) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
      at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1443) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
      at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1423) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_172]
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_172]
      at java.lang.Thread.run(Thread.java:748) [?:1.8.0_172]
  Caused by: org.hibernate.exception.ConstraintViolationException: could not execute batch
      at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:112) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
      at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
      at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
      at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.performExecution(BatchingBatch.java:128) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
      at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.addToBatch(BatchingBatch.java:88) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
      at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3026) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
      at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3547) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
      at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:89) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
      at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:600) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
      at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:474) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
      at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:337) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
      at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
      at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1437) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
      ... 7 more
  Caused by: java.sql.BatchUpdateException: Duplicate entry '1002' for key 'PRIMARY'
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_172]
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[?:1.8.0_172]
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:1.8.0_172]
      at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[?:1.8.0_172]
      at com.mysql.cj.util.Util.handleNewInstance(Util.java:191) ~[mysql-connector-java-8.0.13.jar:8.0.13]
      at com.mysql.cj.util.Util.getInstance(Util.java:166) ~[mysql-connector-java-8.0.13.jar:8.0.13]
      at com.mysql.cj.util.Util.getInstance(Util.java:173) ~[mysql-connector-java-8.0.13.jar:8.0.13]
      at com.mysql.cj.jdbc.exceptions.SQLError.createBatchUpdateException(SQLError.java:224) ~[mysql-connector-java-8.0.13.jar:8.0.13]
      at com.mysql.cj.jdbc.ClientPreparedStatement.executeBatchedInserts(ClientPreparedStatement.java:776) ~[mysql-connector-java-8.0.13.jar:8.0.13]
      at com.mysql.cj.jdbc.ClientPreparedStatement.executeBatchInternal(ClientPreparedStatement.java:447) ~[mysql-connector-java-8.0.13.jar:8.0.13]
      at com.mysql.cj.jdbc.StatementImpl.executeBatch(StatementImpl.java:814) ~[mysql-connector-java-8.0.13.jar:8.0.13]
      at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:2544) ~[c3p0-0.9.5.2.jar:0.9.5.2]
      at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.performExecution(BatchingBatch.java:118) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
      at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.addToBatch(BatchingBatch.java:88) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
      at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3026) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
      at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3547) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
      at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:89) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
      at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:600) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
      at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:474) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
      at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:337) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
      at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
      at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1437) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
      ... 7 more
  </code></pre>
  <p>These are the hibernate settings I am using for config.</p>
  <pre><code>extraProperties.put(Environment.ORDER_UPDATES, true);
  extraProperties.put(Environment.ORDER_INSERTS, true);
  extraProperties.put(Environment.BATCH_VERSIONED_DATA, true);
  extraProperties.put(Environment.USE_SECOND_LEVEL_CACHE, false);
  extraProperties.put(Environment.USE_GET_GENERATED_KEYS, false);
  extraProperties.put(Environment.SHOW_SQL, true);
  extraProperties.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, &quot;thread&quot;);
  extraProperties.put(Environment.STATEMENT_BATCH_SIZE, 100);
  </code></pre>
  <p>Seems like the id is already present in DB.<br>
  Do I need to make any more change for change the <code>GenerationType</code>?<br>
  Any help would be appreciated. Thanks in advance <br></p>