title: ConstraintViolationException while saving entity with @OrderColumn and @OneToMany
body: <p>I have below entity.</p>
  
  <pre><code>@Entity
  public class Shop {
      @OneToMany( cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE }, orphanRemoval = true )
      @JoinTable( name = "shop_image",
              joinColumns = @JoinColumn( name = "shop_id" ),
              inverseJoinColumns = @JoinColumn( name = "image_id" ) )
      @OrderColumn( name = "order_number" )
      @BatchSize( size = 20 )
      private List&lt;Image&gt; images = new ArrayList&lt;Image&gt;( 0 );
  }
  </code></pre>
  
  <p>shop_image table == (id PK, shop_id, image_id, order_number) and has unique constraint (shop_id, image_id).</p>
  
  <p>When below code is invoked (in transactional context):</p>
  
  <pre><code>@PersistenceContext
  EntityManager em;
  
  @Transactional
  // shop exist in DB
  public void reorderImages(Shop shop) {
      //shop.getImages().size == 2; {img0, img1}
      List&lt;Image&gt; newImages = new ArrayList&lt;&gt;();
      newImages.add(shop.getImages().get(1));
      newImages.add(shop.getImages().get(0));
      shop.setImages(newImages); // {img1, img0}
  
      em.merge(shop);
  }
  </code></pre>
  
  <p>I get below exception</p>
  
  <pre><code>com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '5458-98711' for key 'uniqShopAndImage'
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
      at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
      at com.mysql.jdbc.Util.getInstance(Util.java:386)
      at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1039)
      at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
      at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
      at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002)
      at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163)
      at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2624)
      at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2127)
      at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2427)
      at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2345)
      at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2330)
      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:187)
      ... 76 more
  </code></pre>
  
  <p>I know that it is possible to:</p>
  
  <ol>
  <li>Delete all images from shop //shop.getImages().clear()</li>
  <li>em.flush()</li>
  <li>shop.setImages(newImages);</li>
  <li>em.merge(shop)</li>
  </ol>
  
  <p>But I don't like this solution.
  I use hibernate 4 as JPA Provider, MySQL DB. 
  Thanks for your answers.</p>