title: @OrderColumn in Hibernate/JPA results in redundant UPDATE SQL statement
body: <p>I have a simple bi-directional parent/child relationship where the children are in an ordered list.</p>
  
  <p>The parent:</p>
  
  <pre><code>@Entity
  @Table(name = "Parent")
  public class Parent {
      @Id
      private String name;
  
      @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
      @OrderColumn(name = "childIndex")
      private final List&lt;Child&gt; children = new ArrayList&lt;&gt;();
  
      public void setName(final String name) {
          this.name = name;
      }
  
      public void addChild(final Child child) {
          this.children.add(child);
      }
  }
  </code></pre>
  
  <p>The child:</p>
  
  <pre><code>@Entity
  @Table(name = "Child")
  public class Child {
  
      @Id
      @GeneratedValue(generator = "hibernate-uuid")
      @GenericGenerator(name = "hibernate-uuid", strategy = "uuid2")
      private String id;
  
      @ManyToOne(fetch = FetchType.LAZY)
      @JoinColumn(name = "parentName")
      private Parent parent;
  
      Child() {}
  
      public Child(final Parent parent) {
          this.parent = parent;
      }
  }
  </code></pre>
  
  <p>I add an instance of the <code>Parent</code> and <code>Child</code> like so:</p>
  
  <pre><code>SessionFactory sessionFactory = ...
  try (Session session = sessionFactory.openSession()) {
      session.beginTransaction();
  
      Parent parent = new Parent();
      parent.setName("SomeName");
      Child child = new Child(parent);
      parent.addChild(child);
  
      session.merge(parent);
  
      session.getTransaction()
              .commit();
  }
  </code></pre>
  
  <p>This works fine and results in the following SQL:</p>
  
  <pre><code>Hibernate: select parent0_.name as name1_1_1_, children1_.parentName as parentNa2_0_3_, children1_.id as id1_0_3_, children1_.itemIndex as itemInde3_3_, children1_.id as id1_0_0_, children1_.parentName as parentNa2_0_0_ from Parent parent0_ left outer join Child children1_ on parent0_.name=children1_.parentName where parent0_.name=?
  Hibernate: insert into Parent (name) values (?)
  Hibernate: insert into Child (parentName, id) values (?, ?)
  Hibernate: update Child set itemIndex=? where id=?
  </code></pre>
  
  <p>Is it possible to remove the <code>UPDATE</code> statement and instead set the <code>childIndex</code> when the <code>Child</code> row is inserted?</p>
  
  <p>I tried setting <code>nullable=false</code> on the <code>OrderColumn</code> annotation, but that resulted in a <code>null value in column "childindex" violates not-null constraint</code> error, as Hibernate still tries to insert the <code>Child</code> column without setting the <code>childindex</code>.</p>
  
  <p>Thanks in advance!</p>
  
  <p>Edit:</p>
  
  <p>Per Mick's suggestion, I explicitly added a <code>childIndex</code> attribute to the Child entity, like so:</p>
  
  <pre><code>@Column(nullable = false)
  private int childIndex;
  </code></pre>
  
  <p>While this does result in the column becoming non-null, it does not remove the extra <code>UPDATE</code> statement. With this change, Hibernate appears to set the <code>childIndex</code> to 0 on INSERT (no matter what index the child actually is; I've tested this by adding more children), and then performs an UPDATE to correct the index. I'm assuming Hibernate is just using whatever value happens to be on the Child object, which is 0 by default, and then updating it after the INSERT is complete, even if it happens to be the correct value.</p>