title: Foreign key violation with order_inserts=true and batches with mixed subclass entities
body: <h2>Problem</h2>
  <p>Hibernate inserts the entities in the wrong order when <strong>batching</strong> is enabled and <code>hibernate.order_inserts=true</code>. This leads to a foreign key violation. We have one transaction that creates a kind of model with a lot of different entities.
  Everything is saved through cascading including the following entity that gives the foreign key violation:</p>
  <pre><code>public class PlanItemDefinition {
  
      @Id
      @Column(name = &quot;id&quot;)
      @GeneratedValue(generator = &quot;uuid2&quot;)
      @GenericGenerator(name = &quot;uuid2&quot;, strategy = &quot;uuid2&quot;)
      private UUID id;
  
      ...
  
      @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
      @JoinColumn(name = &quot;default_control_id&quot;, foreignKey = @ForeignKey(name = &quot;fk_plan_item_definition_default_control_id&quot;))
      private PlanItemControl defaultControl;
  
      ...
  
  }
  
  
  public class PlanItemControl {
  
      @Id
      @Column(name = &quot;id&quot;)
      @GeneratedValue(generator = &quot;uuid2&quot;)
      @GenericGenerator(name = &quot;uuid2&quot;, strategy = &quot;uuid2&quot;)
      private UUID id;
  
      ...
  
      @OneToOne(mappedBy = &quot;defaultControl&quot;)
      private PlanItemDefinition planItemDefinition;
  
  
      ...
  
  }
  </code></pre>
  <p>Hibernate throws the following exception when the transaction commits:</p>
  <pre><code>Caused by: java.sql.BatchUpdateException: Batch entry 1 insert into model.plan_item_definition 
  (..., default_control_id, ...) values (..., '9572c4fc-4a1b-4747-878e-43d8ec8b8fd6'::uuid, ...) was aborted: ERROR: insert or update on table &quot;plan_item_definition&quot; violates foreign key constraint &quot;fk_plan_item_definition_default_control_id&quot;
    Detail: Key (default_control_id)=(9572c4fc-4a1b-4747-878e-43d8ec8b8fd6) is not present in table &quot;plan_item_control&quot;.  Call getNextException to see other errors in the batch.
      at org.postgresql.jdbc.BatchResultHandler.handleError(BatchResultHandler.java:169)
      at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2286)
      at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:521)
      at org.postgresql.jdbc.PgStatement.internalExecuteBatch(PgStatement.java:870)
      at org.postgresql.jdbc.PgStatement.executeBatch(PgStatement.java:893)
      at org.postgresql.jdbc.PgPreparedStatement.executeBatch(PgPreparedStatement.java:1639)
      at com.zaxxer.hikari.pool.ProxyStatement.executeBatch(ProxyStatement.java:128)
      at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeBatch(HikariProxyPreparedStatement.java)
      at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.performExecution(BatchingBatch.java:118)
      ... 123 more
  Caused by: org.postgresql.util.PSQLException: ERROR: insert or update on table &quot;plan_item_definition&quot; violates foreign key constraint &quot;fk_plan_item_definition_default_control_id&quot;
    Detail: Key (default_control_id)=(9572c4fc-4a1b-4747-878e-43d8ec8b8fd6) is not present in table &quot;plan_item_control&quot;.
      at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2553)
      at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2285)
      ... 130 more
  </code></pre>
  <p>When I remove <code>hibernate.order_inserts=true</code> everything works fine. This seems to be the same problem as <a href="https://hibernate.atlassian.net/browse/HHH-9864?attachmentSortBy=dateTime" rel="nofollow noreferrer">https://hibernate.atlassian.net/browse/HHH-9864?attachmentSortBy=dateTime</a> but since we use hibernate version 5.2.17.Final this should be fixed?</p>
  <h2>Question</h2>
  <p>Can somebody tell me if this is still a hibernate bug or something else we are doing wrong? Does hibernate not consider the order of insert statements in regard to cascading when <code>hibernate.order_inserts=true</code>?</p>