title: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '2' for key 'PRIMARY' during Testing
body: <p>Hi I have this test class which saves 3 entity class to database.
  <strong>Also the first save stores the cast entity twice.</strong></p>
  
  <pre><code>@Test
  //    @Transactional
  public void test() throws ParseException {
      //Creating a movie
      Movie movie1 = new Movie();
      movie1.setMovieName("Movie One");
      movie1.setPlot("Sample Plot1");
      movie1.setReleaseDate(new Date(DateUtils.dateToMilliSecondsConverter(20160101L)));
      //Creating cast
      Cast cast1 = new Cast("male", "lead", Type.MALE_LEAD);
      Cast cast2 = new Cast("female", "lead", Type.FEMALE_LEAD);
      List&lt;Cast&gt; cast = new ArrayList&lt;&gt;();
      cast.add(cast1);
      cast.add(cast2);
      //adding cast to movie
      movie1.setCast(cast);
      movie1.setRuntime(120);
      movieDAO.saveOrUpdate(
      //Creating a movie
      Movie movie2 = new Movie();
      movie2.setMovieName("Movie Two");
      movie2.setPlot("Sample Plot2");
      movie2.setReleaseDate(new Date(DateUtils.dateToMilliSecondsConverter(20160101L)));
      movie2.setRuntime(120);
      movieDAO.saveOrUpdate(movie2);
      CinemaHall ch = new CinemaHall();
      ch.setName("Odeon");
      ch.setMovie(new ArrayList&lt;Movie&gt;() {
      {
          add(movie1);
          add(movie2);
      }
      });
      cinemaHallDAO.saveOrUpdate(ch);
  </code></pre>
  
  <p>}</p>
  
  <p>Entity Class are as follows</p>
  
  <p>CinemaHall.java</p>
  
  <pre><code>public class CinemaHall{
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private long id;
      private String name;
      @OneToMany(targetEntity = Movie.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
      private List&lt;Movie&gt; movie = new ArrayList&lt;&gt;();
  }
  </code></pre>
  
  <p>Movie.Java</p>
  
  <pre><code>public class Movie implements Common {
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private long id;
      private String movieName;
      private Date releaseDate;
      @OneToMany(targetEntity = Cast.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
      private List&lt;Cast&gt; Cast = new ArrayList&lt;&gt;();
      private String plot;
      private Integer runtime;
      @ElementCollection(targetClass = String.class)
      private List&lt;String&gt; showTimings = new ArrayList&lt;&gt;();
  }
  </code></pre>
  
  <p>Cast.Java</p>
  
  <pre><code>public class Cast{
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private long id;
      private String firstName;
      private String lastName;
      @Enumerated(value = EnumType.STRING)
      private Type type;
  }
  </code></pre>
  
  <p>I get the below error when I run the above mentioned test.</p>
  
  <blockquote>
    <p>org.springframework.dao.DataIntegrityViolationException: could not
    execute statement; SQL [n/a]; constraint [null]; nested exception is
    org.hibernate.exception.ConstraintViolationException: could not
    execute statement     at
    org.springframework.orm.hibernate5.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:164)
      at
    org.springframework.orm.hibernate5.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:741)
      at
    org.springframework.orm.hibernate5.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:589)
      at
    org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:761)
      at
    org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:730)
      at
    org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:485)
      at
    org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:291)
      at
    org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
      at
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      at
    org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655)
      at
    com.movie.ticketing.dao.MovieDAO$$EnhancerBySpringCGLIB$$d00001d8.saveOrUpdate()
      at com.movie.ticketing.dao.TestApp.test(TestApp.java:52)    at
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)   at
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)   at
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)   at
    java.lang.reflect.Method.invoke(Unknown Source)   at
    org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
      at
    org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      at
    org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
      at
    org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
      at
    org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
      at
    org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
      at
    org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
      at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)    at
    org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:254)
      at
    org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)  at
    org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)   at
    org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)     at
    org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)   at
    org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)  at
    org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
      at
    org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:309)    at
    org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193)
      at
    org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
      at
    org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      at
    org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
      at
    org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:670)
      at
    org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
      at
    org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
    Caused by: org.hibernate.exception.ConstraintViolationException: could
    not execute statement     at
    org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:59)
      at
    org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
      at
    org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)
      at
    org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97)
      at
    org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:207)
      at
    org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:45)
      at
    org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2921)
      at
    org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3421)
      at
    org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:89)
      at
    org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:560)
      at
    org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:434)
      at
    org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:337)
      at
    org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
      at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1295)
      at
    org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:468)
      at
    org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3135)
      at
    org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2352)
      at
    org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:485)
      at
    org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:147)
      at
    org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$100(JdbcResourceLocalTransactionCoordinatorImpl.java:38)
      at
    org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:231)
      at
    org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:65)
      at
    org.springframework.orm.hibernate5.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:581)
      ... 38 more Caused by:
    com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:
    Duplicate entry '2' for key 'PRIMARY'     at
    sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown
    Source)   at
    sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown
    Source)   at java.lang.reflect.Constructor.newInstance(Unknown Source)
      at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)     at
    com.mysql.jdbc.Util.getInstance(Util.java:387)    at
    com.mysql.jdbc.SQLError.createSQLException(SQLError.java:932)     at
    com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3878)    at
    com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3814)    at
    com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2478)     at
    com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2625)  at
    com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2551)   at
    com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
      at
    com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2073)
      at
    com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2009)
      at
    com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5094)
      at
    com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1994)
      at
    org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
      at
    org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
      at
    org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204)
      ... 56 more</p>
  </blockquote>